jQuery Odd:CSS或jQ选择器? [英] jQuery Odd: CSS or jQ selector?
问题描述
有两种简单的方法可以使用:odd
过滤器或:nth-child奇数)
选择器;
There are two easy ways to select odd rows in a table with jQuery, using the :odd
filter, or the :nth-child(odd)
selector;
$('table tr').filter(':odd')
$('table tr:nth-child(odd)')
每种方法的优点和缺点?
What are the benefits and drawbacks of each method? Mainly wondering in terms of execution speed and browser support.
推荐答案
.filter(' :odd')
最可能更慢,因为它使用一个额外的方法调用,它使用非标准选择器。
The first method with .filter(':odd')
is most probably slower, because it uses an additional method call and it uses a non-standard selector. jQuery has to handle everything in this case.
第二种方法在实现Selectors API的现代浏览器中可能会更快,并且支持:nth -child()
伪类,因为您的给定选择器是有效的CSS,因此选择器将由一个现代浏览器的选择器引擎而不是jQuery来评估。在其他浏览器中,虽然我不知道,但我仍然打赌第二种方法分数更高效,因为至少有一个方法调用。
The second method will potentially be faster in modern browsers that implement the Selectors API and support the :nth-child()
pseudo-class, as your given selector is valid CSS, so the selector will be evaluated by a modern browser's selector engine rather than jQuery. In other browsers, though, I have no idea, but I'd still bet on the second method being fractionally more efficient because there is at least one less method call.
无论哪种方法,任何一种方法都没有真正的(实际的)效益或缺点。实际上,我只是在理论上回答 - 我真的没有任何基准来支持任何东西。也没有必要担心浏览器的兼容性,因为jQuery polyfills :nth-child()
选择器旧版本的浏览器。
Either way, there is no real (practical) benefit or drawback to either method. In fact, I'm only answering theoretically — I don't really have any benchmarks to back anything up. There is also no need to worry about browser compatibility as jQuery polyfills the :nth-child()
selector for older browsers anyway.
这篇关于jQuery Odd:CSS或jQ选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!