javascript - 使用类选择器附加事件处理程序是否会影响该类的动态添加元素?

标签 javascript jquery delegates

我正在使用 jQuery 将函数附加到整个类的单击事件。例如:

$(".clickDiv").click(function(){
   $(this).hide();
});

在我的客户端 JavaScript 上,我动态创建更多 .clickDiv 实例。
我是否需要再次调用 $(".clickDiv).click(function...),或者新实例会自动将该函数绑定(bind)到点击事件吗?

最佳答案

是的,除非您使用委托(delegate)事件

像这样:

$('#container').on('click', '.clickDiv',  function() {
   $(this).hide();
});

docs :

If selector is omitted or is null, the event handler is referred to as direct or directly-bound. The handler is called every time an event occurs on the selected elements, whether it occurs directly on the element or bubbles from a descendant (inner) element.

When a selector is provided, the event handler is referred to as delegated. The handler is not called when the event occurs directly on the bound element, but only for descendants (inner elements) that match the selector. jQuery bubbles the event from the event target up to the element where the handler is attached (i.e., innermost to outermost element) and runs the handler for any elements along that path matching the selector.

Event handlers are bound only to the currently selected elements; they must exist on the page at the time your code makes the call to .on(). To ensure the elements are present and can be selected, perform event binding inside a document ready handler for elements that are in the HTML markup on the page. If new HTML is being injected into the page, select the elements and attach event handlers after the new HTML is placed into the page. Or, use delegated events to attach an event handler, as described next.

Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers. This element could be the container element of a view in a Model-View-Controller design, for example, or document if the event handler wants to monitor all bubbling events in the document. The document element is available in the head of the document before loading any other HTML, so it is safe to attach events there without waiting for the document to be ready.

<小时/>

因为这里太多人建议您应该使用 live,所以自 1.7 版本起,live 已被 on 弃用> 并在版本 1.4.3 中被 delegate

取代
$(selector).live(events, data, handler);                // jQuery 1.3+
$(document).delegate(selector, events, data, handler);  // jQuery 1.4.3+
$(document).on(events, selector, data, handler);        // jQuery 1.7+

关于javascript - 使用类选择器附加事件处理程序是否会影响该类的动态添加元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10068452/

相关文章:

javascript - 切换复选框设置

jquery 脚本第一次不工作

ios - 如何设置不是InterfaceController的HKWorkoutSessionDelegate

C# GetFunctionPointerForDelegate cdecl 而不是 stdcall

javascript - 如何使用方法保存和加载对象?

javascript - 重新订阅 takeUntil/skipUntil

javascript - Meteor.js 中 Node 文件系统 (fs.writeFile) 默认写入何处?

javascript - 如何动态修改 jQuery 多选下拉菜单的背景颜色?

javascript - scrollTop 动画导致屏幕白色闪烁

cocoa - NSOutlineView 轮廓 View SelectionDidChange