我如何学习jQuery选择器在幕后的工作方式? [英] How can I learn how jQuery selectors work behind the scenes?

查看:68
本文介绍了我如何学习jQuery选择器在幕后的工作方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,当我们使用$('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:

  1. jQuery从右到左读取选择器.
  2. 使用精确的ID最快,然后使用精确的标记名,因为它们使用了经过优化的JavaScript原生方法
  3. :first-child这样的本地CSS选择器比:first这样的更快
  1. jQuery reads selectors from right to left.
  2. Using an exact ID is fastest, followed by using an exact tag name, because they use optimized JavaScript-native methods.
  3. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆