简体   繁体   English

什么是选择器引擎?

[英]What is a selector engine?

I've seen news of John Resig's fast new selector engine named Sizzle pop up in quite a few places, but I don't know what a selector engine is, nor have any of the articles given an explanation of what it is. 我已经看到John Resig的快速新选择器引擎Sizzle出现在很多地方的消息,但我不知道选择器引擎是什么,也没有任何文章给出解释它是什么。 I know Resig is the creator of jQuery, and that Sizzle is something in Javascript, but beyond that I don't know what it is. 我知道Resig是jQuery的创建者,Sizzle是Javascript中的东西,但除此之外我不知道它是什么。 So, what is a selector engine? 那么,什么是选择器引擎?

Thanks! 谢谢!

A selector engine is used to query a page's DOM for particular elements, based on some sort of query (usually CSS syntax or similar). 选择器引擎用于基于某种查询(通常是CSS语法或类似的)查询页面的DOM以查找特定元素。

For example, this jQuery: 例如,这个jQuery:

$('div')

Would search for and return all of the <div> elements on the page. 将搜索并返回页面上的所有<div>元素。 It uses jQuery's selector engine to do that. 它使用jQuery的选择器引擎来做到这一点。

Optimizing the selector engine is a big deal because almost every operation you perform with these frameworks is based on some sort of DOM query. 优化选择器引擎是一个大问题,因为几乎每个使用这些框架执行的操作都基于某种DOM查询。

A selector engine is a JavaScript library that lets you select elements in the DOM tree using some kind of string for identifying them (think regular expressions for DOM elements). 选择器引擎是一个JavaScript库,它允许您使用某种字符串选择DOM树中的元素来识别它们(想想DOM元素的正则表达式)。 Most selector engines use some variation of the CSS3 selectors syntax so, for example, you can write something like: 大多数选择器引擎使用CSS3选择器语法的一些变体,例如,您可以编写如下内容:

var paragraphs = selectorengine.select('p.firstParagraph')

to select all P elements in the document with class firstParagraph. 使用类firstParagraph选择文档中的所有P元素。

Some selector engines also support a partial implementation of XPath, and even some custom syntaxes. 一些选择器引擎也支持XPath的部分实现,甚至一些自定义语法。 For example, jQuery lets you write: 例如,jQuery允许您编写:

var checkedBoxes = jQuery('form#login input:checked')

To select all checked check boxes in the login form in the document. 选择文档中登录表单中的所有选中复选框。

A selector engine is a way to traverse the DOM looking for a specific element. 选择器引擎是遍历DOM以查找特定元素的一种方法。

An example of a built in selector engine: 内置选择器引擎的示例:

var foo = document.getElementById('foo');

此外,Sizzle是John Resig目前正致力于取代jQuery已经很棒的选择器引擎的引擎。

A selector engine is used to find elements in a document, in the same way as CSS stylesheets does. 选择器引擎用于查找文档中的元素,与CSS样式表的方式相同。 Currently only Safari has the built-in querySelectorAll function which does just that. 目前只有Safari具有内置的querySelectorAll功能。 With other browser you have to use external JavaScript implementations as LlamaLab Selector or Sizzle instead. 使用其他浏览器,您必须使用外部JavaScript实现作为LlamaLab Selector或Sizzle。

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

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