jQuery stopPropagation 气泡向下

发布于 2024-08-30 18:04:15 字数 482 浏览 4 评论 0原文

我有一个 div,里面有一个链接:

<div id="myDiv">
    <a href="http://www.lol.com">Lol</a>
</div>

单击

应该转到某处,但单击子 应该转到www.lol.com。我从之前的问题jQuery 网站 .stopPropagation 可以防止向上冒泡,但是如何防止向下冒泡(这不是这里所必需的吗?)。

I have a div with a link inside of it:

<div id="myDiv">
    <a href="http://www.lol.com">Lol</a>
</div>

Clicking the <div /> should go somewhere, but clicking the child <a /> should go to www.lol.com. I've seen from previous questions and the jQuery website that .stopPropagation prevents bubbling upwards, but how do I prevent a bubble downwards (isn't that what's necessary here?).

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

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

发布评论

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

评论(2

爱的十字路口 2024-09-06 18:04:15

事件只会冒泡。因此,a 元素的单击事件处理程序在 div 的单击事件处理程序之前触发。如果您想要您描述的行为,则需要向 a 元素添加一个单击事件处理程序,以停止传播到 div。

$("#myDiv a").click( function(event) {
    event.stopPropagation();
} );

并将您拥有的任何事件处理程序保留在 div 上。这应该允许事件执行其默认操作,并防止 div 上的处理程序被触发。

如果您只想阻止点击链接,那么您可以更改 div 元素的事件处理程序

$("#myDiv").click( function( event ) {
    if( !$( event.target ).is( "a" ) )
    {
        // existing event handler
    }
} );

Events only bubble up. So the click event handler for the a element is fired before the click event handler for the div. If you want the behaviour you describe, the you need to add a click event handler to the a element which stops propagation to the div.

$("#myDiv a").click( function(event) {
    event.stopPropagation();
} );

and keep whatever event handler you have on the div. This should allow the event to perform it's default action, and prevent the handler on the div being fired.

If you only want to prevent clicks on links then you can change your event handler for the div element

$("#myDiv").click( function( event ) {
    if( !$( event.target ).is( "a" ) )
    {
        // existing event handler
    }
} );
深海蓝天 2024-09-06 18:04:15

问题是单击锚点仍然会触发

中的单击。这就是所谓的“事件冒泡”。

事实上,有多种解决方案:

在 DIV 单击事件处理程序中检查实际目标元素是否是锚点
→ jsFiddle

$('#myDiv').click(function (evt) {
    if (evt.target.tagName != "A") {
        alert('123');
    }

    // Also possible if conditions:
    // - evt.target.id != "ancherComplaint"
    // - !$(evt.target).is("#ancherComplaint")
});

$("#ancherComplaint").click(function () {
    alert($(this).attr("id"));
});

停止锚点点击侦听器的事件传播 → jsFiddle

$("#ancherComplaint").click(function (evt) {
    evt.stopPropagation();
    alert($(this).attr("id"));
});

正如您可能已经注意到的,我从示例中删除了以下选择器部分:

:not(#ancherComplaint)

这是不必要的,因为没有 .expandable- 类的元素panel-heading 也以 #ancherComplaint 作为其 ID。

我假设您想抑制锚点的事件。这不能以这种方式工作,因为两个选择器(你的和我的)选择完全相同的 DIV。选择器被调用时对监听器没有影响;它仅设置侦听器应注册到的元素列表。由于这个列表在两个版本中都是相同的,因此不存在差异。

The problem was that clicking the anchor still triggered a click in your <div>. That's called "event bubbling".

In fact, there are multiple solutions:

Checking in the DIV click event handler whether the actual target element was the anchor
→ jsFiddle

$('#myDiv').click(function (evt) {
    if (evt.target.tagName != "A") {
        alert('123');
    }

    // Also possible if conditions:
    // - evt.target.id != "ancherComplaint"
    // - !$(evt.target).is("#ancherComplaint")
});

$("#ancherComplaint").click(function () {
    alert($(this).attr("id"));
});

Stopping the event propagation from the anchor click listener → jsFiddle

$("#ancherComplaint").click(function (evt) {
    evt.stopPropagation();
    alert($(this).attr("id"));
});

As you may have noticed, I have removed the following selector part from my examples:

:not(#ancherComplaint)

This was unnecessary because there is no element with the class .expandable-panel-heading which also have #ancherComplaint as its ID.

I assume that you wanted to suppress the event for the anchor. That cannot work in that manner because both selectors (yours and mine) select the exact same DIV. The selector has no influence on the listener when it is called; it only sets the list of elements to which the listeners should be registered. Since this list is the same in both versions, there exists no difference.

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