div事件穿透~~~

冰川711 2011-07-03 10:33:36
现有两个DIV 是嵌套关系, 分别都有单击事件,,

单击内层DIV 空白处, 内层DIV的事件未被执行, 外层的DIV 事件被执行了,,

有没有什么好的解决办法
...全文
251 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lsw645645645 2011-07-04
  • 打赏
  • 举报
回复
冒泡问题
bustersword 2011-07-04
  • 打赏
  • 举报
回复
恩,不错,是嵌套,差点看成是层覆盖.....
zell419 2011-07-03
  • 打赏
  • 举报
回复

<script>
function test(e,a){
e = e || window.event;
if (window.event) {
e.cancelBubble = true;
} else {
e.stopPropagation();
}
if(!a) alert("外");
else{alert("内"); }
}
</script>
<div style="background: #666699;width: 300px;height: 200px;" onclick="test(event)">
<div onclick="test(event,1)" style="background: #66EE99;width: 100px;height: 80px;">
</div>
</div>

是不是这意思 ?
冰川711 2011-07-03
  • 打赏
  • 举报
回复
就是说 我要我结果是 单击内层DIV空白处也执行 内层DIV的事件 不执行外层的
weike021996 2011-07-03
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 aspwebchh 的回复:]

HTML code
<div style="width:100px; height:100px; border:1px solid #000" id="x">
<div style="width:50px; height:50px; margin:0 auto; border:1px solid #ccc" id="y"></div>
</div>
<script type="text/jav……
[/Quote] 正解啊,防止事件冒泡 学习了
挨踢直男 2011-07-03
  • 打赏
  • 举报
回复
<div style="width:100px; height:100px; border:1px solid #000" id="x">
<div style="width:50px; height:50px; margin:0 auto; border:1px solid #ccc" id="y"></div>
</div>
<script type="text/javascript">
document.getElementById("x").onclick=function(e)
{
alert("x")
e = e || window.event;
  if (e.stopPropagation )
   e.stopPropagation();
  else
   e.cancelBubble = true;


}
document.getElementById("y").onclick=function(e)
{
alert("y")
e = e || window.event;
  if (e.stopPropagation )
   e.stopPropagation();
  else
   e.cancelBubble = true;
}
</script>

阻止事件冒泡

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧