网络编程
位置:首页>> 网络编程>> JavaScript>> vue实现在v-html的html字符串中绑定事件

vue实现在v-html的html字符串中绑定事件

作者:sayok_why  发布时间:2023-07-02 16:44:08 

标签:vue,v-html,html,字符串,绑定事件

需求:

需要在v-html的html字符串的button中绑定点击事件,需要点击后做一些操作,必须渲染成html,但是渲染后的html里面写绑定事件的代码没有经过vue编译,所以事件无效。


<div class="code-review">
 <div v-html="html" v-highlight @click="addComment($event)"></div>
</div>

computed: {
 html () {
  return '<button></button >'
 },
},

解决办法:

在v-html同级元素中使用事件绑定,然后根据事件触发的目标对象去判断和获取参数。


addComment:function (event) {
if(event.target.nodeName === 'BUTTON'){
// 获取触发事件对象的属性
alert("a");
}
},

来源:https://blog.csdn.net/sayoko06/article/details/86737467

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com