新元素的onLoad,委托/开启? [英] onLoad of new element, delegate/on?
问题描述
我正在尝试在动态加载的元素( .this_button
)上运行函数。我使用以下代码:
Im trying to run a function on an element (.this_button
) that is being loaded dynamically. Im using the following code:
$(function(){
$("body").on("load", ".this_button", function() {
console.log("its been loaded");
});
});
我尝试了 delegate
,但是它说弃用了 on
。例如,在文档已加载10分钟后,可能会推送某些元素。如何连续检查元素 .this_button
是否已进入正文?
I tried delegate
, but it says its been deprecated in favor of on
. Some elements might be pushed, say after the document's been loaded for 10 minutes already. How can it continually check if an element .this_button
has entered the body?
谁知道为什么这不起作用?
Anyone know why this isnt working?
推荐答案
来自文档:
在所有浏览器中,加载,滚动和错误事件(例如,在元素上)不会冒泡。[...]此类事件不支持与委托一起使用,但是可以将事件处理程序直接附加到生成事件的元素上时使用。
"In all browsers, the load, scroll, and error events (e.g., on an element) do not bubble. [...] Such events are not supported for use with delegation, but they can be used when the event handler is directly attached to the element generating the event."
这篇关于新元素的onLoad,委托/开启?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!