您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

jquery:delegate重复触发事件问题的解决办法

2025/9/4 11:54:07发布31次查看
jquery:delegate重复触发事件问题的解决办法
<html> <head> <script type="text/javascript" src="http://www.w3school.com.cn/jquery/jquery.js"></script> <style> div{border:1px solid balck;padding:5px;margin:2px;} </style> </head> <body> <div id="div1" >div1 <div id="div1_1" >div1_1 <div id="div1_1_1" >div1_1_1 <span style="background:red; display:block; width:25px; height:25px;">span</span> </div> </div> <div id="div1_2" >div1_2 </div> </div> </body> <script type="text/javascript"> $('body').delegate('body>div','click',function(){alert("div");}); $('body').delegate('div>span','click',function(){alert("span");}); </script> </html>
请问如何避免在点击span区域时不会触发div点击事件???
$('body').delegate('div>span','click',function(evt){ evt.stoppropagation(); alert("span"); } );
取消span的click事件冒泡,1楼已给出解答:e.stoppropagation()~
方法1.阻止事件冒泡
方法2.判断事件触发者,
$('body').delegate('body>div','click',function(){ if(e.target!=$('#div1_1_1').find('span')[0]) alert("div"); });

方法1.阻止事件冒泡
方法2.判断事件触发者,
$('body').delegate('body>div','click',function(){ if(e.target!=$('#div1_1_1').find('span')[0]) alert("div"); });

$('body').delegate('body>div','click',function(e){ if(e.target!=$('#div1_1_1').find('span')[0]) alert("div"); });
我试过了
以上就是jquery:delegate重复触发事件问题的解决办法的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product