美文网首页
【基础】阻止事件冒泡

【基础】阻止事件冒泡

作者: JerichoPH | 来源:发表于2017-10-12 21:55 被阅读4次

阻止事件冒泡

  • html
<div @click="show2()">
    <button @click="show($event)">阻止事件冒泡</button>
</div>
  • js
<script>
    // 初始化数据
    window.onload = function () {
        // vue
        var vueApp = new Vue({
            el: '#app',
            data: {message: '这里是测试内容'},
            methods: {
                show: function (event) {
                    alert(1);
                    // 由于使用了cancelBubble = true 阻止事件冒泡,所以则不会执行show2()
                    event.cancelBubble = true;
                },
                show2: function (message) {
                    alert(2);
                }
            }
        });
    };
</script>

相关文章

网友评论

      本文标题:【基础】阻止事件冒泡

      本文链接:https://www.haomeiwen.com/subject/vrfayxtx.html