JavaScript/vue.js/点击阻止
来自康健生活
< JavaScript | vue.js
标题文字
点击事件,阻止子元素事件触发父元素事件
event.currentTarget
event.currentTarget获取到的是click事件绑定的DOM对象,event.target获取到的为当前所点击的DOM对象。在父元素中判断event.currentTarget == event.target是否为true
let eve = event.currentTarget;
let eve2 = event.target;
if(eve === eve2){
...
}
@click.stop
子元素绑定阻止冒泡的点击事件
function (e) {
e.stopPropagation();//阻止事件冒泡即可
//e.cancelBubble=true;//非标准的IE方式:; 这里的cancelBubble是 IE事件对象的属性,设为true就可以了
}
function (e) {
//事件处理代码
//实际上是终结了这个(点击)事件,冒泡当然也就停止了。
return false;
}
elementui
使用 @click 失效,改称 @click.native
<template>
<div id="NavMenu" @click="navMenuHref(this)">
<div class="box box-content">
<el-menu class="b-nav el-menu-vertical"
@open="handleOpen"
@close="handleClose"
@click.native="stopClick(this)"
navMenuHref() {
this.stopPropagation();
},
stopClick(e) {
this.stopPropagation();
},
stopPropagation(e) {
e = e || window.event;
if (e.stopPropagation) { //W3C阻止冒泡方法
e.stopPropagation();
} else {
e.cancelBubble = true; //IE阻止冒泡方法
}
}
No categories