if you have the latest version of jquery, is livequery still useful?
From what I understand livequery is f开发者_StackOverflow中文版or maintaining your events after DOM changes.
Does not the latest build of jquery already support this?
Yes, it is still useful. live()
only works on certain events, while livequery()
can be bound to any event the user's browser provides.
http://docs.jquery.com/Events/live
Possible event values:
click, dblclick, mousedown, mouseup, mousemove, mouseover, mouseout, keydown, keypress, keyup
Currently not supported:
blur, focus, mouseenter, mouseleave, change, submit
Note that also unsupported are touch events like touchstart, touchend
, etc.
One useful feature that livequery()
provides while live()
doesn't is the ability to fire a custom function every time a new element is matched (and/or an element is no longer matched).
From the docs:
Live Query also has the ability to fire a function (callback) when it matches a new element and another function (callback) for when an element is no longer matched. This provides ultimate flexibility and untold use-cases. For example the following code uses a function based Live Query to implement the jQuery hover helper method and remove it when the element is no longer matched.
$('li')
.livequery(function(){
// use the helper function hover to bind a mouseover and mouseout event
$(this)
.hover(function() {
$(this).addClass('hover');
}, function() {
$(this).removeClass('hover');
});
}, function() {
// unbind the mouseover and mouseout events
$(this)
.unbind('mouseover')
.unbind('mouseout');
});
精彩评论