网页 综合 更新时间 2026-11-13 17:11:51

Home · jquery/sizzle Wiki · GitHub

自动蜘蛛 sizzlejs.com 正文约 4927 字
Home · jquery/sizzle Wiki · GitHub
Project Home Page | Source Repository | Google Group Discussion
Note: Browser support may differ between standalone Sizzle and libraries that include Sizzle. Please report issues on Sizzle's issue tracker, rather than the trackers for disparate libraries.
1Workarounds for Internet Explorer 6 are still in the code but the browser is no longer actively tested.
To report a bug in any of these browsers, please add an issue with a test case from jsfiddle or jsbin.
Sizzle supports virtually all CSS 3 Selectors, including escaped selectors (.foo\+bar), Unicode selectors, and results returned in document order. The only exceptions are those that would require additional DOM event listeners to keep track of the state of elements.
As such, the following pseudo-selectors are not supported:
Note: These CSS3 pseudo-selectors were unsupported prior to version 1.9:
Note: In this context, input, button, select, and textarea are all considered to be input elements.
In this context, "positional" refers to an element's placement in the collection after a selection, based on document order. For example, div:first would return an array containing the first div on the page, while div:first em would target the first div on the page and select all em elements within.
Note: Positional indexes begin at zero.
The Sizzle API consists of 3 parts:
The main function for finding elements. Uses querySelectorAll if available.
returns (Array): All elements matching the selector
selector: A CSS selector
context: An element, document, or document fragment to use as the context for finding elements. Defaults to document. Note: Prior to version 2.1, document fragments were not valid here.
results: An array or an array-like object, to which Sizzle will append results. For example, jQuery passes a jQuery collection. An "array-like object" is an object with a nonnegative numeric length property and a push method.
Uses native matchesSelector if available
returns(Boolean): Whether the given element matches the selector
element: A DOMElement against which Sizzle will test the selector
selector: A CSS selector
returns(Array): Elements in the array that match the given selector
selector: A CSS selector
elements: An array of DOMElements to filter against the specified selector
This contains the regular expressions used to parse a selector into different parts, to be used for finding and filtering. The name of each of the regular expressions should correspond to the names specified in the Sizzle.selectors.find and Sizzle.selectors.filter objects.
In order to add a new find function:
A method for finding some elements on a page. The specified function will be called no more than once per selector.
In order to add a new filtering statement:
An optional pre-filter function which allows filtering of the matched array against the corresponding regular expression, which will return a new matched array. This matched array will eventually be passed and flattened as arguments against the corresponding filter function. This is intended to clean up some of the repetitive processing that occurs in a filter function.
Note: match[0] will be deleted prior to being passed to a filter, and must not be used.
The arguments for a filter method are the element and the captures from the regex corresponding to this filter (indicated above by what is in the match, starting at index 1). The return result must be boolean: true if the element matches the selector, false if not.
Handle an attribute which requires specialized processing (such as href, which has cross-browser issues). The return result must be the actual string value of that attribute.
The most common extension to a selector engine: adding a new pseudo. The return result from this function must be boolean: true if the element matches the selector, false if not.
For example, this defines a simple :fixed pseudo:
createPseudo is only required if the custom pseudo-selector accepts an argument.
Note: In jQuery 1.8 and earlier, the API for creating custom pseudos with arguments was broken. In jQuery 1.8.1+, the API is backwards-compatible. Regardless, the use of createPseudo is greatly encouraged.
Now that the parser compiles a single function containing other functions, custom pseudo-selectors with arguments are much cleaner.
For example, within Sizzle, the implementation of the :not( <sub-selector> ) pseudo is very similar to:
In order to write a custom selector with arguments that can take advantage of the new API, yet still support all versions of Sizzle, check for the createPseudo method.
The following example uses jQuery syntax. Live example
These filters are run after a previous part of a selector has already returned results. setFilters are found from matching Sizzle.selectors.match.POS. When applicable, argument is expected to be an integer. The not argument is a boolean indicating whether the result should be inverted (as in div:not(:first)).
访问原文 返回搜索 https://sizzlejs.com/
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。