Jquery - 将委托绑定到所有“a”标签

发布于 2024-12-09 20:54:30 字数 877 浏览 3 评论 0原文

当您单击 div 中的链接时,我尝试阻止 jq click()

HTML

<div id="all">
    <div id="test">
        <a href="http://google.de">google</a>
    </div>
</div>

JS

$('#test').click(function() { alert('only when i click the div');  });

$('a').click(function(e) {
    e.stopPropagation();
    e.preventDefault();
    $('body').append(e.target.href);
});       

这段代码效果很好,但我的内容是动态的,所以我需要一个 delegate() 解决方案。

下面的代码不起作用。但为什么?有什么问题吗?

$('#all').delegate("a", "click", function(e)
{
    e.stopPropagation();
    e.preventDefault();
    $('body').append(e.target.href);
});

示例
http://jsfiddle.net/Lf3hL/13/

i try to block the jq click() when you click on an link in a div.

HTML

<div id="all">
    <div id="test">
        <a href="http://google.de">google</a>
    </div>
</div>

JS

$('#test').click(function() { alert('only when i click the div');  });

$('a').click(function(e) {
    e.stopPropagation();
    e.preventDefault();
    $('body').append(e.target.href);
});       

This code works great but my content is dynamically so i need a delegate() solution.

The code below dont work. But why? Whats the problem?

$('#all').delegate("a", "click", function(e)
{
    e.stopPropagation();
    e.preventDefault();
    $('body').append(e.target.href);
});

example
http://jsfiddle.net/Lf3hL/13/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

好菇凉咱不稀罕他 2024-12-16 20:54:30

stopPropagation 不适用于委托 - http://api.jquery.com/event.stopPropagation/

由于 .live() 方法在事件传播到文档顶部后才对其进行处理,因此无法停止实时事件的传播。类似地,由 .delegate() 处理的事件将传播到它们被委托的元素;调用委托事件处理程序时,绑定在 DOM 树中其下方任何元素上的事件处理程序都已经执行。因此,这些处理程序可能会通过调用 event.stopPropagation() 或返回 false 来阻止委托处理程序触发。

现在,如果您更改测试点击以使用委托并使用 stopImmediatePropagation 它将起作用

$('#all').delegate('#test', 'click' ,function() {
    alert('only when i click on the div');
});    

$('#all').delegate("a", "click", function(e)
{
    e.stopImmediatePropagation();
    e.preventDefault();
    $('body').append(e.target.href);
}); 

http://jsfiddle.net/Lf3hL/14/

stopPropagation doesn't work with delegate - http://api.jquery.com/event.stopPropagation/

Since the .live() method handles events once they have propagated to the top of the document, it is not possible to stop propagation of live events. Similarly, events handled by .delegate() will propagate to the elements to which they are delegated; event handlers bound on any elements below it in the DOM tree will already have been executed by the time the delegated event handler is called. These handlers, therefore, may prevent the delegated handler from triggering by calling event.stopPropagation() or returning false.

Now if you change your test click to use a delegate and use stopImmediatePropagation it will work

$('#all').delegate('#test', 'click' ,function() {
    alert('only when i click on the div');
});    

$('#all').delegate("a", "click", function(e)
{
    e.stopImmediatePropagation();
    e.preventDefault();
    $('body').append(e.target.href);
}); 

http://jsfiddle.net/Lf3hL/14/

-柠檬树下少年和吉他 2024-12-16 20:54:30

因为 delegate() 的工作方式是让事件冒泡,然后在祖先元素上处理它们(在示例中为 #all)。

因此,由于事件已经冒泡,因此您无法阻止它们在源头传播(因为它们必须传播才能工作)。

Because delegate() works by letting events bubble up, then handling them on the ancestor element (#all in your example).

So, because the events have already bubbled up, you can't stop them propagating at their source (because they must propagate to work).

阳光下慵懒的猫 2024-12-16 20:54:30

来自 jQuery .delegate() 文档

由于 .live() 方法在事件传播到
在文档的顶部,不可能停止传播
现场活动。类似地,由 .delegate() 处理的事件将传播
给他们被委托的部门;绑定的事件处理程序
DOM 树中位于其下方的所有元素都已被执行
当调用委托事件处理程序时。

一种可能的解决方案是不在 a 上附加事件处理程序,而是在 #test 上附加事件处理程序,并检查 event.target

我已经用这种方法更新了你的Fiddle

   $('#test').click(function(e) {
        if ($(e.target).is('a')) {
            alert('link was clicked');
        }
        else {
            alert('only when i click on the div');
        }

        e.preventDefault(); //just to cancel the link's default action
    });

From the jQuery .delegate() doc:

Since the .live() method handles events once they have propagated to
the top of the document, it is not possible to stop propagation of
live events. Similarly, events handled by .delegate() will propagate
to the elements to which they are delegated; event handlers bound on
any elements below it in the DOM tree will already have been executed
by the time the delegated event handler is called.

One possible solution is not attaching the event handler on the a, but on #test instead, and checking the event.target.

I've updated your Fiddle with this method.

   $('#test').click(function(e) {
        if ($(e.target).is('a')) {
            alert('link was clicked');
        }
        else {
            alert('only when i click on the div');
        }

        e.preventDefault(); //just to cancel the link's default action
    });
岁月无声 2024-12-16 20:54:30

live 和 delegate 函数无法像“正常”事件一样处理 e.stopPropagation(); 调用。来自委托 docs -

由于 .live() 方法在事件传播到
在文档的顶部,不可能停止传播
现场活动。类似地,由 .delegate() 处理的事件将传播
给他们被委托的部门;绑定的事件处理程序
DOM 树中位于其下方的所有元素都已被执行
调用委托事件处理程序时。这些处理者,
因此,可能会阻止委托处理程序触发
调用 event.stopPropagation() 或返回 false。

The live and delegate functions are not able to handle the e.stopPropagation(); call in the same way 'normal' events would. From the delegate docs -

Since the .live() method handles events once they have propagated to
the top of the document, it is not possible to stop propagation of
live events. Similarly, events handled by .delegate() will propagate
to the elements to which they are delegated; event handlers bound on
any elements below it in the DOM tree will already have been executed
by the time the delegated event handler is called. These handlers,
therefore, may prevent the delegated handler from triggering by
calling event.stopPropagation() or returning false.

三生一梦 2024-12-16 20:54:30

为什么不使用该

.live()

功能?

 $('a').live('click', function() {
     // Live handler called.
 });

这将为正文完全加载后添加的所有标签添加点击功能。

why not use the

.live()

function?

 $('a').live('click', function() {
     // Live handler called.
 });

that would add a click function to all the tags that are added after the body is fully loaded.

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