gpt4 book ai didi

jQuery on() 方法不像 live() 那样绑定(bind)事件

转载 作者:行者123 更新时间:2023-12-03 21:31:31 24 4
gpt4 key购买 nike

http://api.jquery.com/live/ 中所述:

As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers.

对。 所以而不是

$('.dynamicallyCreatedElement').live('click', function(){
console.log('click');
});

我应该使用:

$('.dynamicallyCreatedElement').on('click', function(){
console.log('click');
});

但是,它不会将事件绑定(bind)到调用 on() 后创建的元素。那么 live() 方法真的更好吗?

我错过了什么吗?

最佳答案

使用onlive 相同的方式用来工作你需要像这样使用它:

$(document).on("click", ".dynamicallyCreatedElement", function() {   
console.log('click');
});

因此,您将 on 处理程序绑定(bind)到 document 本身(或者实际上,新元素将“出现”的容器元素 - 感谢 @devnull69为了澄清),然后向其传递事件类型和选择器。

您会在 live 的中间找到几个示例。文档页面。

关于jQuery on() 方法不像 live() 那样绑定(bind)事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10295399/

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