vue在v-html的html字符串中绑定事件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了vue在v-html的html字符串中绑定事件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2358字,纯文字阅读大概需要4分钟。
内容图文
最近做项目,使用得vue作为基础框架。有这样一个需求,需要给返回来的富文本绑定事件,搜了一下众说纷纭。自己摸索了和总结出一套比较好用得方式。
利用JavaScript事件捕获,通过给使用v-html的标记绑定click事件,捕获到点击的标记。从而实现vue动态绑定dom元素事件的效果;
代码如下:
富文本数据:
"richTextContent": "<p class=‘testssss‘ data-innervalue=‘sadsadsa‘ data-value=‘{nasdlkashdashdkjsahdksd}‘><span style=\"font-size: 14px;\"></span>Some Wi?Fi networks are open and you can simply connect for internet access. Other Wi?Fi networks are secure and require you to enter a password. Contact the Wi-Fi network's owner for the password.</p><p>To search for and connect with available Wi-Fi networks:</p><ol class=\" list-paddingleft-2\"><li><p>Swipe down from the top of your screen to open <strong>quick settings</strong> and then:</p></li><ul class=\" list-paddingleft-2\"><li><p>Android 9: Touch & hold <img src=\"https://help.motorola.com/hc/images/global/qs_wifi_on_90.png\"/>.</p></li><li><p>Android 8: Touch <strong>Wi Fi > More settings</strong></p></li><li><p>Android 7: Touch <strong>Wi Fi</strong>.</p></li></ul><li><p>Turn Wi-Fi on <img src=\"https://help.motorola.com/hc/images/global/slider_on.png\"/>.</p></li><li><p>Touch the Wi-Fi network you want to connect to.</p></li><li><p>If the network is a secure one, enter the password.</p></li></ol><p>Make sure you enter the password correctly. Touch <strong>Show password</strong> to see what you’re typing.</p><p>When your phone is connected, <img src=\"https://help.motorola.com/hc/images/global/i_wifi_min.png\"/> appears in the status bar. If the connection is poor, <img src=\"https://help.motorola.com/hc/images/global/i_wifi_poor.png\"/> appears in the status bar.</p>",
vue模板写法
< div class ="richText" v-html ="richTextContent" @click ="test" ></ div >
script方法
test (event) { console.log(event.target.className) // testssss console.log(event.target.nodeName) // pif (event.target.nodeName === ‘p‘ && event.target.className === ‘testssss‘) { // 获取触发事件对象的属性 alert(‘a‘) } }
最终效果
原文:https://www.cnblogs.com/wangweizhang/p/12067700.html
内容总结
以上是互联网集市为您收集整理的vue在v-html的html字符串中绑定事件全部内容,希望文章能够帮你解决vue在v-html的html字符串中绑定事件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。