gpt4 book ai didi

jquery - 动态添加到 DOM 的元素的单击事件

转载 作者:行者123 更新时间:2023-12-03 22:55:36 25 4
gpt4 key购买 nike

在 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.

关于jquery - 动态添加到 DOM 的元素的单击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15868661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com