最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is livequery deprecated - Stack Overflow

programmeradmin2浏览0评论

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(){

    });
});

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(){

    });
});
Share Improve this question asked Oct 6, 2011 at 14:10 sameoldsameold 19.2k22 gold badges65 silver badges87 bronze badges 6
  • 4 yes, livequery is dead. It's also an anti pattern – Raynos Commented Oct 6, 2011 at 14:17
  • 2 @Raynos What replaces it? As of yet I've found no examples that serve its purpose. .on is for events, and the only jquery event that comes close is DOMNodeInserted which doesn't have wide browser support. – AaronLS Commented Feb 4, 2013 at 20:44
  • 1 @AaronLS event delegation will solve your problem. If you need DOMNodeInserted your either building a complex templating system or your doing it wrong. If the former then just build a simple templating system instead – Raynos Commented Feb 5, 2013 at 3:18
  • 4 @Raynos Wow really? You just made a whole host of invalid assumptions. I've seen plenty of valid uses of livequery and have yet to see an alternative that preserves separation of concerns. There are in deed initialization scenarios when dealing with dynamic content, particularly in single page applications, that are not tied to a specific event, and therefore event delegation will NOT solve the problem. If you believe otherwise, then provide an example. I've encountered the "you're doing it wrong" kind before, and you leave out any viable solution because your goal is to put others down. – AaronLS Commented Feb 5, 2013 at 17:42
  • 4 @Raynos Here's a use case - a UserScript. I don't control the code that inserts elements into the page but I'd like my script to know when they are inserted. I have yet to see any other way to accomplish this. – Nathan Osman Commented Dec 2, 2015 at 19:24
 |  Show 1 more comment

2 Answers 2

Reset to default 15

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.

$('.some_class').livequery( function() {

       // apply a plugin to the element
    $(this).somePlugin();

}, function() {

    // clean up after the element was removed

});

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.

You have to use on() and attach the event to a parent or body. Ex :

$('#obj').livequery('click', function() { ... });
$('#obj').livequery(function() { ... });

become

$('body').on('click', '#obj', function() { ... });
$('body').on('DOMNodeInserted','#obj', function() { ... });

note that DOMNodeInserted is IE9+

发布评论

评论列表(0)

  1. 暂无评论