阻止JavaScript事件冒泡传递(cancelBubble 、stopPropagation)
阻止JavaScript事件冒泡传递(cancelBubble 、stopPropagation)
发布时间:2016-12-30 来源:查字典编辑
摘要:cancelBubble在IE下有效stopPropagation在Firefox下有效复制代码代码如下:阻止JavaScript事件冒泡传...

cancelBubble在IE下有效

stopPropagation在Firefox下有效

复制代码 代码如下:

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml"lang="gb2312">

<head>

<title>阻止JavaScript事件冒泡传递(cancelBubble、stopPropagation)</title>

<metaname="keywords"content="JavaScript,事件冒泡,cancelBubble,stopPropagation"/>

<scripttype="text/javascript">

functiondoSomething(obj,evt){

alert(obj.id);

vare=(evt)?evt:window.event;

if(window.event){

e.cancelBubble=true;

}else{

//e.preventDefault();

e.stopPropagation();

}

}

</script>

</head>

<body>

<divid="parent1"onclick="alert(this.id)"style="width:250px;background-color:yellow">

<p>Thisisparent1div.</p>

<divid="child1"onclick="alert(this.id)"style="width:200px;background-color:orange">

<p>Thisischild1.</p>

</div>

<p>Thisisparent1div.</p>

</div>

<br/>

<divid="parent2"onclick="alert(this.id)"style="width:250px;background-color:cyan;">

<p>Thisisparent2div.</p>

<divid="child2"onclick="doSomething(this,event);"style="width:200px;background-color:lightblue;">

<p>Thisischild2.Willbubble.</p>

</div>

<p>Thisisparent2div.</p>

</div>

</body>

</html>

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新Javascript教程学习
热门Javascript教程学习
编程开发子分类