4 ms·
I'm not convinced that there's two loops -- aren't the .css() methods chained together, and would be executed right after each other for each step through the i
by pixl8ed 14y ago
I'm not convinced that there's two loops -- aren't the .css() methods chained together, and would be executed right after each other for each step through the implied loop?
In
$(".widget").css("color", "green").css("font-size", "16px");
does the color of all the widgets change, and then the fontSize, or does the color/fontSize change together for each widget?
I suspect the latter...
- thezilch 14y agoChained functions call independently and completely before returning to the next. The css function [0] operates on the "array" this returned by $ using an anonymous function [1] at a call site in the access function [1] [2]; access then returns the object formerly known as this [3]. Chain, rinse, and repeat to your heart's content. [0] https://github.com/jquery/jquery/blob/master/src/css.js#L102 https://github.com/jquery/jquery/blob/master/src/css.js#L102 [1] https://github.com/jquery/jquery/blob/master/src/css.js#L103 https://github.com/jquery/jquery/blob/master/src/css.js#L103 [2] https://github.com/jquery/jquery/blob/master/src/core.js#L750 https://github.com/jquery/jquery/blob/master/src/core.js#L75... [3] https://github.com/jquery/jquery/blob/master/src/core.js#L756 https://github.com/jquery/jquery/blob/master/src/core.js#L75...
- pixl8ed 14y agoDoh -- of course... Many thanks for the explanation & the links!
- sim0n 14y agoThere must obviously be two loops, jQuery can't perform magic. The first .css() call will iterate over the cached array of elements, apply the color attributes and then return a reference to the jQuery instance. The second call will loop once again and this time apply the font-size attribute. There is no extra DOM lookups, but there will certainly be two loops.