jquery live 在 A 标签上返回 false

发布于 2024-12-13 13:26:40 字数 367 浏览 3 评论 0原文

我有一个点击实时绑定到一个元素。处理程序执行后返回 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一枫情书 2024-12-20 13:26:40

您正在寻找的是 preventDefault()

$('*').live('click', function(e){
  // lots of code here
  e.preventDefault();
  return false; // Don't think this is necessary
});

我还想说,附加一个对所有元素进行事件可能不是一个好主意。

通过专门附加到 A 标记,您会得到更好的服务:

$('a').click(function(e){
    // lots of code here
    e.preventDefault();
    return false;
});

What you're looking for is preventDefault()

$('*').live('click', function(e){
  // lots of code here
  e.preventDefault();
  return false; // Don't think this is necessary
});

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:

$('a').click(function(e){
    // lots of code here
    e.preventDefault();
    return false;
});
甜妞爱困 2024-12-20 13:26:40

我不会将点击函数绑定到 *。您可以将其绑定到 body 元素;单击将从被单击的元素传播到 body 元素。您可以像这样检查最初单击的元素:

$("body").live("click", function (e) {
    if ($(e.originalEvent.target).is("a")) {
        return false;
    }
});

话虽如此,您仍然可以这样做:

$("*").live("click", function () {
    if ($(this).is("a") == false) {
        return false;
    }
});

I would not bind a click function to *. You could bind it to the body element; the click will propagate from the element that was clicked to the body element. You can check the element that was originally clicked like this:

$("body").live("click", function (e) {
    if ($(e.originalEvent.target).is("a")) {
        return false;
    }
});

Having said that, you can still do this:

$("*").live("click", function () {
    if ($(this).is("a") == false) {
        return false;
    }
});
遗心遗梦遗幸福 2024-12-20 13:26:40

尝试:

if($(this).tagName == 'A')
    return true; // perhaps even { do nothing }
else
    return false;

这可能不是获取标记名的确切代码,请参阅此 如何扩展 jQuery 以便更轻松地检索 tagName

另请参阅:http://fuelyourcoding.com/jquery-events-stop -misusing-return-false/

Try:

if($(this).tagName == 'A')
    return true; // perhaps even { do nothing }
else
    return false;

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文