Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

83
Visualizações
append div with additional functon

I have an odd task. I am simply trying to add an additonal function on an element when its clicked. I cant seem to trigger the addScroll function that is found within my methods. As you can see, the div should be appended to the screen when the button is clicked. Once that div is clicked as well, the remove method is applied and I also want to apply the addScrollBack().

new Vue({
  el: "#app",
  data: {
    chocs: [
      { text: "Learn JavaScript", done: false },
      { text: "Learn Vue", done: false },
      { text: "Play around in JSFiddle", done: true },
      { text: "Build something awesome", done: true }
    ]
  },
  methods: {
  addScrollBack: function(){
  alert("test");
  },
handlePosterClick: function(choc){
alert("ckciked")

     window.top.$(".2l-body").css("overflow","hidden");
    
$("#cook").append(`<div style="background-color:blue;height:200px; width:300px" onclick="document.querySelector('#popover-div').remove();addScrollBack();>test</div>`);
   
   },
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <div id="cook">

  </div>
  <button v-on:click="handlePosterClick(choc)">
        Book
        </button>
</div>

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Not clear, but suggestion is use v-html instead of append

new Vue({
  el: "#app",
  data: {
    cook: "Old content OR blank",
    chocs: [
      { text: "Learn JavaScript", done: false },
      { text: "Learn Vue", done: false },
      { text: "Play around in JSFiddle", done: true },
      { text: "Build something awesome", done: true }
    ]
  },
  methods: {
  addScrollBack: function(){
  alert("test");
  },
handlePosterClick: function(choc){

//alert("ckciked " + choc)
// window.top.$(".2l-body").css("overflow","hidden");


// inline Scripting not recommanded,
 this.cook= `<div style="background-color:blue;height:200px; width:300px" >test</div>`;
   
   },
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <div id="cook" v-html="cook">
    
  </div>
  <button v-on:click="handlePosterClick('choc')">
        Book
        </button>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

Look at this :

https://codepen.io/hl037/pen/QWOjwyK

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  
  {{displayDiv}}
  <div id="cook">
    <div
      v-if="displayDiv"
      style="background-color:blue;height:200px; width:300px"
      v-on:click="onDivClicked"
    >
      test
    </div>
  </div>
  <button v-on:click="displayDiv=true">
    Book
  </button>
</div>
new Vue({
  el: "#app",
  data: {
    displayDiv:false,
    chocs: [
      { text: "Learn JavaScript", done: false },
      { text: "Learn Vue", done: false },
      { text: "Play around in JSFiddle", done: true },
      { text: "Build something awesome", done: true },
    ],
  },
  methods: {
    onDivClicked(){
      /*document.querySelector('#popoverdiv').remove();*/
      this.addScrollBack();
    },
    addScrollBack: function(){
      alert("test");
    },
    handlePosterClick: function(choc){
    },
  }
})

...Instead of manipulating the dom yourself, just use vue's template, with a model controlling its behavior. The same applies to your other component (the popup) : it should instead listen on a custom event on this component to add/remove scroll using reactivity.

Using vanilla javascript for dom modification, event-handling etc. is NOT recommended with vue. Always use vue's way of doing it. vanilla dom modification should only be done by experts, mostly to interface with non-vue code.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda