我如何学习jQuery选择器在幕后的工作方式? [英] How can I learn how jQuery selectors work behind the scenes?
问题描述
例如,当我们使用$('div span')
时,jQuery如何为我们做这样的搜索?
For example, when we use $('div span')
, how does jQuery do such a search for us?
它首先搜索span
元素还是div
?
似乎jQuery官方网站对此没有解释;它只是向我们展示了API可以提供哪些功能.
It seems that the jQuery official site has no explanation on these things; it just show us what's functions are available through the API.
我想知道每种类型的jQuery选择器是如何实现的,因此我可以选择要使用的最佳选择器.
I want to know how every type jQuery selector is implemented, so I can select the best selector to use.
你知道我在哪里可以得到这样的信息吗?
Do you know where I can get such info?
推荐答案
广泛地讲,您需要了解的内容:
Broadly, what you need to know:
- jQuery从右到左读取选择器.
- 使用精确的ID最快,然后使用精确的标记名,因为它们使用了经过优化的JavaScript原生方法
- 像
:first-child
这样的本地CSS选择器比:first
这样的更快
- jQuery reads selectors from right to left.
- Using an exact ID is fastest, followed by using an exact tag name, because they use optimized JavaScript-native methods.
- Native CSS selectors like
:first-child
are faster than jQuery selectors like:first
.
您还可以阅读 Sizzle官方文档,但它没有.真正讨论优化技术.
You can also read the official Sizzle documentation, but it doesn't really discuss optimization techniques.
这篇关于我如何学习jQuery选择器在幕后的工作方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!