jquery – 动态添加到DOM的元素的Click事件

前端之家收集整理的这篇文章主要介绍了jquery – 动态添加到DOM的元素的Click事件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在jQuery 1.8之前,我可以使用.live()在点击按钮时触发,该按钮由jquery动态插入.

现在,.on()和.bind()都不适用于加载页面添加到DOM的元素.

现在有哪些选择?

解决方法

$(parent_element).on("click",child_selector,function(evt) {
});

http://api.jquery.com/on/#direct-and-delegated-events

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.

原文链接:https://www.f2er.com/jquery/181403.html

猜你在找的jQuery相关文章