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;
}
}

猜你喜欢

转载自www.cnblogs.com/sx00/p/10914135.html