jquery live 在 A 标签上返回 false
我有一个点击实时绑定到一个元素。处理程序执行后返回 false,但如果该元素恰好是 A 标签,则链接也不会被跟踪,这是不可接受的...
$('*').live('click', function(){
// lots of code here
return false;
});
现在,如果单击的元素具有类似 A>img 的路径
并且点击图像,A 永远不会被跟随。我不希望点击事件进一步传播。
我该如何实现这一目标?
编辑:
对于我的应用程序来说,将事件绑定到 * 至关重要。此外,可以在任何元素上单击,而不仅仅是 A。因此,上述解决方案都不适合我希望实现的目标。
I have a click bound as live to an element. The handler returns false after execution, but if the element happens to be an A tag, then the link is also not followed, which is unacceptable...
$('*').live('click', function(){
// lots of code here
return false;
});
Now if the element that is clicked on has a path like A>img
and the image is clicked on, the A will never be followed. I dont want the click event to propagate any further.
how do I achieve this?
Edit:
It is essential for my application to bind the event to *. Also, the clicks can be made on any element, not just A's. Thus none of the mentioned solutions are suitable for what I wish to achieve.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在寻找的是 preventDefault()
我还想说,附加一个对所有元素进行事件可能不是一个好主意。
通过专门附加到
A
标记,您会得到更好的服务:What you're looking for is preventDefault()
I'd also like to say that attaching an event to all elements is probably Not A Good Idea.
You'd be better served by attaching to the
A
tags specifically:我不会将点击函数绑定到
*
。您可以将其绑定到body
元素;单击将从被单击的元素传播到body
元素。您可以像这样检查最初单击的元素:话虽如此,您仍然可以这样做:
I would not bind a click function to
*
. You could bind it to thebody
element; the click will propagate from the element that was clicked to thebody
element. You can check the element that was originally clicked like this:Having said that, you can still do this:
尝试:
这可能不是获取标记名的确切代码,请参阅此 如何扩展 jQuery 以便更轻松地检索 tagName。
另请参阅:http://fuelyourcoding.com/jquery-events-stop -misusing-return-false/
Try:
This might not be the exact code to get the tagname, see this How to extend jQuery to make it easier to retrieve the tagName.
Also see this: http://fuelyourcoding.com/jquery-events-stop-misusing-return-false/