开发者

Hiding an element using jQuery; which method should I use and why?

What is the difference b开发者_开发技巧etween these two methods?

$("div span").hide();

$("div > span").hide();

Any impact on performance?


The first will hide all <span>s found anywhere under the <div>. The second will only hide <span>s that are immediate children of the <div>.

jQuery child-selector


On the performance aspect, it may depend upon the exact HTML, but I found the div > span selector to be about 30% faster in this jsperf test. That may be because it only has to look in the children of each div rather than through the whole DOM.

But, it's probably not enough of a speed difference to matter in most cases.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜