防止click事件影响父jquery
我要停止从子级到父级的事件传播,我有一堆包含 a
的 li
标签。
$('li a[rel=close]').live('click', function(e){
e.stopPropagation();
e.preventDefault();
})
但这并不能阻止活动。有什么建议吗?
I was to stop the event propagation from the child to the parent, i have a bunch of li
tags containing a
.
$('li a[rel=close]').live('click', function(e){
e.stopPropagation();
e.preventDefault();
})
But it doesn;t stop the event.Any suggestions ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
stopPropagation
在live
方面存在问题,来自 jQuery stopPropagation 文档 -正如 Rob W 所说,您的代码可以与
bind
一起正常工作,这里有一个演示 - http: //jsfiddle.net/TmKyT/stopPropagation
has problems withlive
, from the jQuery stopPropagation docs -As Rob W has said your code would work fine with
bind
, here's a demo - http://jsfiddle.net/TmKyT/使用
.bind
而不是.live
。live
事件在传播树的末尾触发。仅当您还想为稍后创建的元素绑定事件侦听器时,live
才比bind
更有用。Use
.bind
instead of.live
. Thelive
event is triggered at the end of the propagation tree.live
is only more useful thanbind
when you want to also bind the event listener for elements which are created later.也许尝试使用委托?
Maybe try using delegate?