jquery - 不需要的传播效应

发布于 2024-12-15 10:23:42 字数 665 浏览 2 评论 0原文

我有一个现场听众在课堂上添加了一些内容。 这以某种方式重复调用该侦听器。在这种情况下如何关闭传播?

SomeContent = "<p>Some content<p>";
moreContent  = "<p>More content<p>";

$('.myClass').live('change', function (event){                  
    var thisOption = $(this);                               

    // event.preventDefault(); - does not work              
    // event.stopImmediatePropagation(); - does not work

    var MyLocation = thisOption.closest('tr').next('tr');               
    // $(MyLocation).html("");                  
    var thisTplReasons = $(moreContent);
    thisTplReasons.appendTo(MyLocation).page();

    return false;
});     

I have a live listener on a class which adds some content.
This is somehow calling this listener repeatedly. How can I turn off propagatio in this case?

SomeContent = "<p>Some content<p>";
moreContent  = "<p>More content<p>";

$('.myClass').live('change', function (event){                  
    var thisOption = $(this);                               

    // event.preventDefault(); - does not work              
    // event.stopImmediatePropagation(); - does not work

    var MyLocation = thisOption.closest('tr').next('tr');               
    // $(MyLocation).html("");                  
    var thisTplReasons = $(moreContent);
    thisTplReasons.appendTo(MyLocation).page();

    return false;
});     

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

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

发布评论

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

评论(2

酒浓于脸红 2024-12-22 10:23:42

jQuery live 函数有一些缺陷,记录在此处。列表中有趣的部分是这个声明

在事件处理程序中调用 event.stopPropagation() 无法有效停止附加在文档下方的事件处理程序;该事件已传播到文档。

您应该“on”使用新的 jQuery 机制(请参阅此处)而不是“live”和调用event.stopPropagation() 应该按预期工作。

The jQuery live function has some flaws documented here. The interesting part of the list is this statement

Calling event.stopPropagation() in the event handler is ineffective in stopping event handlers attached lower in the document; the event has already propagated to document.

You should use the new jQuery mechanism "on" (see here) instead of "live" and a call to event.stopPropagation() should work as expected.

夜无邪 2024-12-22 10:23:42

事件首先被捕获,直到到达目标元素,然后向上冒泡。在事件流期间,可以通过引发操作和/或停止事件(对于符合 W3C 的浏览器,使用 event.stopPropagation() 方法,在任一阶段对路径中的任何元素(观察者)响应事件命令 event.cancelBubble = true(对于 Internet Explorer),和/或通过取消事件的默认操作。

来源 wiki

另外,

jQuery says

event.stopPropagation()

描述:防止事件在 DOM 树中冒泡,从而防止通知任何父处理程序事件的。

希望这有帮助。

Events are first captured until it reaches the target element, and then bubbled up. During the event flow, an event can be responded to at any element in the path (an observer) in either phase by causing an action, and/or by stopping the event (with method event.stopPropagation() for W3C-conforming browsers and command event.cancelBubble = true for Internet Explorer), and/or by cancelling the default action for the event.

Source wiki

Also,

jQuery says

event.stopPropagation()

Description: Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event.

Hope this helps.

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