简体   繁体   English

使用 jQuery 隐藏元素; 我应该使用哪种方法,为什么?

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

What is the difference between 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> .第一个将隐藏在<div>下的任何位置找到的所有<span> > 。 The second will only hide <span> s that are immediate children of the <div> .第二个只会隐藏<span>的直接子级的<div>

jQuery child-selector jQuery 儿童选择器

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 .在性能方面,它可能取决于确切的 HTML,但我发现div > span选择器在这个jsperf 测试中快了大约 30%。 That may be because it only has to look in the children of each div rather than through the whole DOM.这可能是因为它只需要查看每个 div 的子项,而不是查看整个 DOM。

But, it's probably not enough of a speed difference to matter in most cases.但是,在大多数情况下,速度差异可能还不够重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM