I can visualize the correct answer based on theory, but I am just looking for some confirmation. I'm wondering what is the most effective way to reuse jQuery's selected element? For example:
$ ('# my_div'). CSS ('background', 'red'); // Some other code $ ('# my_div'). Attr ('name', 'red div'); versus
myDiv = $ ('# my_div'); MyDiv.css ('background', 'red'); // some other code myDiv.attr ('name', 'red div'); I think the second example is more efficient because the element #my_div does not have to be received more than once. Is it true?
Similarly, it is more efficient to save $ (it) in a favor like 'obja', and then instead of using more than $ (this), 'obja' Reuse? In this case, jQuery is not being forced to find an element repeatedly, but it is being forced to convert it to a jQuery object [$ (this)]. So as a general rule of thumb, should any jQuery object be always stored in a variable, if it is used more than once?
If you select jQuery selector ( like $ ('# element') ) If yes, you should always store your results. If you use the object and wrap it in jQuery (like $ (this) ), it is not necessary, because jQuery is able to search again for that element is not needed.
Comments
Post a Comment