I got a question about live method
$('.classname').live('click',function() { some code; });
Is there some problem of performance if a have ab开发者_StackOverflow中文版out 100-200 or 300 elements with this class?
It depends heavily on the contents of { some code }
Only way to tell is to set it up and test. If the performance is adequate, you have no problem. If performance is poor, then you can profile it in Firebug and/or Chrome/Safari Developer Tools, and attempt to further optimize the bound function.
live
and delegate
should actually improve performance for large volumes of elements. That is of course in comparison to bind
. If live
is slow, you can practically guarantee that bind
will be worse.
The reason for this is that a delegate
call* stores the function on a single element, while a bind
call stores a copy of the function on every element.
When you use a delegate, it stores the listener on a parent element that will catch the event and call the callback in the context of the child element.
Don't optimize prematurely. If you've already got issues, try separating out code into reusable chunks and see how much you can reduce. If you don't have issues, then what's the problem?
* live
is essentially $(document).delegate(selector,...)
精彩评论