jquery - 不需要的传播效应
我有一个现场听众在课堂上添加了一些内容。 这以某种方式重复调用该侦听器。在这种情况下如何关闭传播?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
jQuery live 函数有一些缺陷,记录在此处。列表中有趣的部分是这个声明
您应该“on”使用新的 jQuery 机制(请参阅此处)而不是“live”和调用event.stopPropagation() 应该按预期工作。
The jQuery live function has some flaws documented here. The interesting part of the list is this statement
You should use the new jQuery mechanism "on" (see here) instead of "live" and a call to event.stopPropagation() should work as expected.
事件首先被捕获,直到到达目标元素,然后向上冒泡。在事件流期间,可以通过引发操作和/或停止事件(对于符合 W3C 的浏览器,使用 event.stopPropagation() 方法,在任一阶段对路径中的任何元素(观察者)响应事件命令 event.cancelBubble = true(对于 Internet Explorer),和/或通过取消事件的默认操作。
来源 wiki
另外,
jQuery says
描述:防止事件在 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
Description: Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event.
Hope this helps.