How to bind a click event on “v-html” in Vue

后端 未结 1 734
情书的邮戳
情书的邮戳 2021-02-05 12:59

I have a simple example in jsfiddle, as described in the example, I want to insert the element via v-html and then bind the event in the insert element. In addition to adding id

1条回答
  •  青春惊慌失措
    2021-02-05 13:21

    You can add a ref on your div, and operate with its children elements like you do in regular JavaScript. For example, you can set an event listener for a link inside mounted hook:

    var app = new Vue({
      el: '#app',
      data: {
        link: 'click me'
      },
      mounted() {
        this.$refs['mydiv'].firstChild.addEventListener('click', function(event) {
          event.preventDefault();
          console.log('clicked: ', event.target);
        })
      }
    })
    
    

    0 讨论(0)
提交回复
热议问题