简体   繁体   English

在jQuery中使用选择器的最有效方法?

[英]Most efficient way to use selectors in jQuery?

is it more efficient to use $('.active') or $('div.active') ? 使用$('.active')$('div.active')更有效率? I have always avoided including "div" because it's extra text in the javascript file I don't want the user to have to download. 我一直避免使用“div”,因为它是javascript文件中的额外文本我不希望用户必须下载。

Older versions of IE will benefit from the inclusion of div because they don't support getElementsByClassName() . 较旧版本的IE将受益于包含div因为它们不支持getElementsByClassName()

Because of that, every element on the page needs to be selected with: 因此,需要选择页面上的每个元素:

document.getElementsByTagName('*');

...and manually tested to see if it has the active class. ...并手动测试以查看它是否具有active类。

If you include div , then it is able to narrow it down a bit, since it can do: 如果你包含div ,那么它可以缩小一点,因为它可以做到:

document.getElementsByTagName('div');

...then test only those elements. ...然后只测试那些元素。

When I say older versions , I mean IE6 and IE7 since IE8+ supports querySelectorAll . 当我说旧版本时 ,我的意思是IE6和IE7,因为IE8 +支持querySelectorAll


EDIT: 编辑:

Browser suppport: 浏览器支持:

It depends. 这取决于。 If you mean performance. 如果你的意思是表现。 I prepared special test for everyone on JSPerf: jquery class selector test . 我在JSPerf:jquery类选择器测试中为每个人准备了特殊测试 On my browser and computer (FF 3.6.13 and Core 2 Duo 1.6) div.active is a bit slower. 在我的浏览器和计算机上(FF 3.6.13和Core 2 Duo 1.6) div.active有点慢。 But found it variable - it seems GC has something doing here. 但发现它变量 - 似乎GC在这里有所作为。

And after few more tests it seems that div.active : 经过几次测试后,似乎div.active

  • Speed is variable on FF, sometimes GC turns on 'div.active' test, but generally difference is very small. 速度在FF上是可变的,有时GC打开'div.active'测试,但通常差异非常小。
  • Unnoticable difference on Chrome and Safari Chrome和Safari上的区别显而易见
  • Faster on IE9 在IE9上更快

I like to include the tag name if it helps self-document the code. 我喜欢包含标签名称,如果它有助于自我记录代码。 If I can use 如果我可以使用

$("nav.primary") 

instead of 代替

// this is the primary nav
$(".primary")

I tend to do it. 我倾向于这样做。

I guess the best way to get some speed on large pages is to use find instead. 我想在大页面上获得一些速度的最佳方法是使用find代替。

$( your-container ).find("div.active") $(你的容器).find(“div.active”)

Since you always? 既然你总是? know where you should look, you can create your own scope. 知道你应该在哪里看,你可以创建自己的范围。 So that the browser only need to search within that area of code. 这样浏览器只需要在该代码区域内进行搜索。

By the way, you don't need to care about size of the css, EVER :) Use css minifing tools to minimize the css when the site is in production mode. 顺便说一句,你不需要关心css的大小,永远:)使用css minifing工具来最小化网站处于生产模式时的CSS。 You can also set your web server to automatically gzip your css files before sending the to the user. 您还可以将Web服务器设置为在发送给用户之前自动gzip您的css文件。 And if you don't change your css filename on every pageload, the browser cache up to whole css file. 如果您不在每个页面加载上更改您的css文件名,浏览器将缓存到整个css文件。

CSS selectors in jQuery used to be optimized similar to how you would do it for browsers, see: http://css-tricks.com/efficiently-rendering-css/ jQuery中的CSS选择器过去经过优化,与浏览器的方式类似,请参阅: http//css-tricks.com/efficiently-rendering-css/

Specifying a generic tag anywhere, even with an ID or class would be dramatically slower than just specifying the ID or class alone. 在任何地方指定通用标记,即使使用ID或类,也会比单独指定ID或类要慢得多。 See: 看到:

http://www.no-margin-for-errors.com/demos/how-to-optimize-jquery-selectors/ http://www.no-margin-for-errors.com/demos/how-to-optimize-jquery-selectors/

The above uses jQuery 1.3. 以上使用jQuery 1.3。 Since jQuery 1.4 and the introduction of the Sizzling selector engine, this is less important from what I understand. 自从jQuery 1.4和Sizzling选择器引擎的引入以来,根据我的理解,这并不重要。 See: 看到:

http://hungred.com/useful-information/jquery-optimization-tips-and-tricks/ http://hungred.com/useful-information/jquery-optimization-tips-and-tricks/

For myself, I decided in CSS to use whatever reads the easiest, and I am more specific there since that is only parsed once. 对于我自己,我决定在CSS中使用最简单的读取,我更具体,因为它只解析了一次。 In jQuery, however, I have been more careful since those selectors could run thousands of times over the life of a page. 然而,在jQuery中,我更加小心,因为这些选择器可以在页面的整个生命周期中运行数千次。

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

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