阻止冒泡,阻止默认事件(阻止捕获)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<ul id="b">
<li id="a">A</li>
<li>A</li>
<li>A</li>
<li>A</li>
<a href="http://caibaojian.com/javascript-stoppropagation-preventdefault.html" id="c">访问</a>
</ul>
<script>
window.onload=function () {
var a=document.getElementById("a");
var b=document.getElementById("b");
a.onclick=function () {
alert("xxx");
window.event? window.event.cancelBubble = true : e.stopPropagation();//阻止冒泡
}
b.onclick=function () {
alert("bbbb");
}
c.onclick=function (e) { //阻止浏览器的默认行为,但会冒泡
if ( e && e.preventDefault ){
e.preventDefault();
}
//IE中阻止函数器默认动作的方式
else{
window.event.returnValue = false;
}
return false;

}
}
</script>
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/cycczh/p/11666702.html