js冒泡,阻止冒泡
js 冒泡事件 阻止冒泡
window.onload = function () {
var oDiv1 = document.getElementById('div1');
var oDiv2 = document.getElementById('div2');
oDiv1.onclick = function (ev){
var oEvent = ev || event;
alert("this is div1");
//js阻止事件冒泡
//oEvent.cancelBubble = true;
//oEvent.stopPropagation();
//js阻止链接默认行为,没有停止冒泡
//oEvent.preventDefault();
//return false;
}
oDiv2.onclick = function (ev){
var oEvent = ev || event;
alert("this is div2");
oEvent.cancelBubble = true;
}
}
///////////////--------- demo -------------------------------------------
<div style=" background-color: red; height: 200px;width: 300px;top: 400px;left: 400px;position: absolute;" onclick="wai()">
12312313
<div onclick="nei()">
33333333333333333333333
</div>
</div>
function wai() {
console.log(" wai ")
}
function nei() {
window.event ? window.event.cancelBubble = true : e.stopPropagation();
console.log(" nei ")
}