livequery 是否已弃用
我正在看旧代码。我发现对于使用 ajax 添加的元素,有很多 livequery 代码。新版本的 jquery 不再需要 livequery 了吗?有谁知道哪个版本之后不再需要它?
$("#somediv").livequery(function(){
$(this).click(function(){
});
});
I'm looking at old code. I'm seeing that for elements that get added with ajax, there's lots of livequery code. Is livequery not needed anymore with the newer versions of jquery? Does anyone know after which version exactly it's not needed?
$("#somediv").livequery(function(){
$(this).click(function(){
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
livequery
与.live()
是一个完全不同的概念。.live()
方法使用事件委托来处理页面上任何位置发生的事件。livequery
将在 DOM 发生更改时调用处理程序(通过 jQuery 方法)。对于下面的示例,当将带有
class="some_class"
的元素添加到 DOM(或将类添加到元素)时,第一个处理程序将运行。移除后,是第二个。实际应该很少需要
livequery
,但在极少数情况下,您需要响应 DOM 更改,并且无法控制导致这些更改的 jQuery,它可能有用。livequery
is an entirely different concept from.live()
.The
.live()
method uses event delegation to handle events that occur anywhere on the page.livequery
will invoke handlers when DOM changes occur (via jQuery methods).For the example below, when an element with
class="some_class"
is added to the DOM (or the class is added to an element), the first handler will run. When removed, the second.There should be little actual need for
livequery
, but in that rare case where you need to respond to DOM changes, and have no control over the jQuery that is causing those changes, it can be useful.您必须使用 on() 并将事件附加到父级或正文。例如:
注意
DOMNodeInserted 是 IE9+
You have to use on() and attach the event to a parent or body. Ex :
become
note that DOMNodeInserted is IE9+