Is there a difference between $ (element) .find (". Class"). Css (...) and $ (this) .css inside $ (element) .find (". Class"). Each () function?

If I want to edit the CSS properties of a DOM element with a specific class using the jQuery search method, does it matter (in performance and browser compatibility) if I use this shortcut:

$(domobject).find(".theclass").css("color","#FFF");

or every function like this:

$(domobject).find(".theclass").each(function() {
    $(this).css("color","#FFF");
});

In my case, the dom-object is an element of backbone.js view.el, but of course my question is not limited to backbone.js only.

Thank you for your help!

+5
source share
3 answers

In terms of performance, the first fragment is slightly faster :

enter image description here

, jQuery, , , .each.

, .

+2

, "theclass"?

, jQuery Documentation .each() :

. jQuery, jQuery, jQuery - , . , .each()

, :)

+3

The first choice is better - less code, an unnecessary anonymous function is not created, there are no calls to this function. Both are browsers. jQuery: write less, do more.

+2
source

All Articles