|
| 1 | +<?xml version="1.0"?> |
| 2 | +<entry> |
| 3 | + <title>jQuery.find()</title> |
| 4 | + <desc>The jQuery selector engine, formerly known as Sizzle, is exposed under <code>jQuery.find</code>. This page describes all the APIs under <code>jQuery.find</code>. |
| 5 | + </desc> |
| 6 | + <longdesc> |
| 7 | + <h2>Note</h2> |
| 8 | + |
| 9 | + <p>jQuery selector engine first tries to run the passed selector — with some modifications — through native <code>querySelectorAll</code>, so selectors natively supported by the current browsers generally work out of the box. However, if the browser does not recognize the selector, jQuery does the whole selection on its own. Some newer selectors may not work with such a jQuery selection.</p> |
| 10 | + |
| 11 | + <h2>Selectors</h2> |
| 12 | + |
| 13 | + <h3>CSS 3</h3> |
| 14 | + |
| 15 | + <p>jQuery supports virtually all <a href="http://www.w3.org/TR/css3-selectors/">CSS 3 Selectors</a>, including escaped selectors (<code>.foo\+bar</code>), 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.</p> |
| 16 | + |
| 17 | + <p>As such, the following pseudo-selectors are <em>not</em> supported:</p> |
| 18 | + <ul> |
| 19 | + <li><code>:hover</code></li> |
| 20 | + <li><code>:active</code></li> |
| 21 | + <li><code>:visited</code>, <code>:link</code></li> |
| 22 | + </ul> |
| 23 | + |
| 24 | + <p><strong>Note</strong>: These CSS3 pseudo-selectors were unsupported prior to version 1.9:</p> |
| 25 | + <ul> |
| 26 | + <li><code>:target</code></li> |
| 27 | + <li><code>:root</code></li> |
| 28 | + <li><code>:nth-last-child</code></li> |
| 29 | + <li><code>:nth-of-type</code>, <code>:nth-last-of-type</code>, <code>:first-of-type</code>, <code>:last-of-type</code>, <code>:only-of-type</code></li> |
| 30 | + <li><code>:lang()</code></li> |
| 31 | + </ul> |
| 32 | + |
| 33 | + <h3>Other selectors and conventions</h3> |
| 34 | + |
| 35 | + <h4>Changes</h4> |
| 36 | + |
| 37 | + <ul> |
| 38 | + <li>Full selector lists in <code>:not()</code>; e.g. <code>:not(a.b)</code>, <code>:not(div > p)</code>, <code>:not(div, p)</code></li> |
| 39 | + <li>Nested pseudo-selectors; e.g. <code>:not(:has(div:first-child))</code></li> |
| 40 | + </ul> |
| 41 | + |
| 42 | + <h4>Additions</h4> |
| 43 | + |
| 44 | + <ul> |
| 45 | + <li><code>[NAME!=VALUE]</code>: Elements whose NAME attribute doesn't match the specified value. Equivalent to <code>:not([NAME=VALUE])</code>.</li> |
| 46 | + <li><code>:contains(TEXT)</code>: Elements with textContent containing the word 'TEXT'. Case-sensitive.</li> |
| 47 | + <li><code>:header</code>: Header elements (h1, h2, h3, h4, h5, h6).</li> |
| 48 | + <li><code>:parent</code>: Elements with at least one child node (either text or an element).</li> |
| 49 | + <li><code>:selected</code>: (option) elements that are currently selected</li> |
| 50 | + </ul> |
| 51 | + |
| 52 | + <h4>Form Selector Additions</h4> |
| 53 | + |
| 54 | + <p><strong>Note</strong>: In this context, <code>input</code>, <code>button</code>, <code>select</code>, and <code>textarea</code> are all considered to be input elements.</p> |
| 55 | + |
| 56 | + <ul> |
| 57 | + <li><code>:input</code>: Input elements</li> |
| 58 | + <li><code>:button</code>: Input elements that are <code>button</code>s or have type "button"</li> |
| 59 | + <li><code>:checkbox</code>, <code>:file</code>, <code>:image</code>, <code>:password</code>, <code>:radio</code>, <code>:reset</code>, <code>:submit</code>, <code>:text</code>: Input elements with the specified type</li> |
| 60 | + </ul> |
| 61 | + |
| 62 | + <h4>Positional Selector Additions</h4> |
| 63 | + |
| 64 | + <p>In this context, "positional" refers to an element's placement in the collection after a selection, based on document order. For example, <code>div:first</code> would return an array containing the first <code>div</code> on the page, while <code>div:first em</code> would target the first <code>div</code> on the page and select all <code>em</code> elements within.</p> |
| 65 | + |
| 66 | + <p><strong>Note</strong>: Positional indexes begin at zero.</p> |
| 67 | + |
| 68 | + <ul> |
| 69 | + <li><code>:first</code>/<code>:last</code>: The first/last matching element</li> |
| 70 | + <li><code>:even</code>/<code>:odd</code>: Even/odd-numbered elements</li> |
| 71 | + <li><code>:eq</code>/<code>:nth</code>: The nth element; e.g. <code>:eq(5)</code> finds the 6th element</li> |
| 72 | + <li><code>:lt</code>/<code>:gt</code>: Elements at positions above/below the specified position</li> |
| 73 | + </ul> |
| 74 | + |
| 75 | + <h2>API</h2> |
| 76 | + |
| 77 | + <p>The jQuery Selection API consists of 3 parts:</p> |
| 78 | + |
| 79 | + <ul> |
| 80 | + <li>The <strong><a href="#jquery-find-public-api">Public API</a></strong>, which users interact with.</li> |
| 81 | + <li>The <strong><a href="#jquery-find-extension-api">Extension API</a></strong>, for modifications to the selector engine.</li> |
| 82 | + <li>The <strong><a href="#jquery-find-internal-api">Internal API</a></strong>, used internally by jQuery.</li> |
| 83 | + </ul> |
| 84 | + |
| 85 | + <h3 id="jquery-find-public-api">Public API</h3> |
| 86 | + |
| 87 | + <h4><code>jQuery.find( String selector[, DOMNode context[, Array results]] )</code></h4> |
| 88 | + |
| 89 | + <p>The main function for finding elements. Uses <code>querySelectorAll</code> if available.</p> |
| 90 | + |
| 91 | + <p><strong>returns</strong> (Array): All elements matching the selector</p> |
| 92 | + |
| 93 | + <p><em>Parameters</em></p> |
| 94 | + |
| 95 | + <p><strong>selector</strong>: A CSS selector</p> |
| 96 | + |
| 97 | + <p><strong><em>context</em></strong>: An element, document, or document fragment to use as the context for finding elements. Defaults to <code>document</code>. |
| 98 | + <strong>Note:</strong> Prior to version 2.1, document fragments were not valid here.</p> |
| 99 | + |
| 100 | + <p><strong><em>results</em></strong>: An array or an array-like object, to which jQuery will append results. For example, jQuery passes a jQuery collection. An "array-like object" is an object with a nonnegative numeric <code>length</code> property and a <code>push</code> method.</p> |
| 101 | + |
| 102 | + <h4><code>jQuery.find.matchesSelector( DOMElement element, String selector )</code></h4> |
| 103 | + |
| 104 | + <p>Uses native <code>matchesSelector</code> if available</p> |
| 105 | + |
| 106 | + <p><strong>returns</strong>(Boolean): Whether the given element matches the selector</p> |
| 107 | + |
| 108 | + <p><em>Parameters</em></p> |
| 109 | + |
| 110 | + <p><strong>element</strong>: A <code>DOMElement</code> against which jQuery will test the selector</p> |
| 111 | + |
| 112 | + <p><strong>selector</strong>: A CSS selector</p> |
| 113 | + |
| 114 | + <h4><code>jQuery.find.matches( String selector, Array<DOMElement> elements )</code></h4> |
| 115 | + |
| 116 | + <p><strong>returns</strong>(Array): Elements in the array that match the given selector</p> |
| 117 | + |
| 118 | + <p><em>Parameters</em></p> |
| 119 | + |
| 120 | + <p><strong>selector</strong>: A CSS selector</p> |
| 121 | + |
| 122 | + <p><strong>elements</strong>: An array of <code>DOMElements</code> to filter against the specified selector</p> |
| 123 | + |
| 124 | + <h3 id="jquery-find-extension-api">Extension API</h3> |
| 125 | + |
| 126 | + <h4><code>jQuery.expr.match.NAME = RegExp</code></h4> |
| 127 | + |
| 128 | + <p>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 <code>jQuery.expr.find</code> and <code>jQuery.expr.filter</code> objects.</p> |
| 129 | + |
| 130 | + <h4>Finding</h4> |
| 131 | + |
| 132 | + <p>In order to add a new find function:</p> |
| 133 | + <ul> |
| 134 | + <li>A regular expression must be added to the <code>match</code> object.</li> |
| 135 | + <li>A function to <code>find</code> must be defined.</li> |
| 136 | + <li><code>"|" + NAME</code> must be appended to the <code>jQuery.expr.order</code> regex.</li> |
| 137 | + </ul> |
| 138 | + |
| 139 | + <h5><code>jQuery.expr.find.NAME = function( match, context, isXML ) {}</code></h5> |
| 140 | + |
| 141 | + <p>A method for finding some elements on a page. The specified function will be called no more than once per selector.</p> |
| 142 | + <ul> |
| 143 | + <li><code>match</code> is the array of results returned from matching the specified regex against the selector.</li> |
| 144 | + <li><code>context</code> is the DOMElement or DOMDocument from which selection will occur.</li> |
| 145 | + <li><code>isXML</code> is a boolean value indicating whether the function is currently operating within an XML document.</li> |
| 146 | + </ul> |
| 147 | + |
| 148 | + <h4>Filtering</h4> |
| 149 | + |
| 150 | + <p>In order to add a new filtering statement:</p> |
| 151 | + <ul> |
| 152 | + <li>A regular expression must be added to the <code>match</code> object.</li> |
| 153 | + <li>A function must be added to the <code>filter</code> object.</li> |
| 154 | + <li>A function may optionally be added to the <code>preFilter</code> object.</li> |
| 155 | + </ul> |
| 156 | + |
| 157 | + <h5><code>jQuery.expr.preFilter.NAME = function( match ) {}</code></h5> |
| 158 | + |
| 159 | + <p>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.</p> |
| 160 | + |
| 161 | + <h5><code>jQuery.expr.filter.NAME: function( element, match[1][, match[2], match[3], ...] ) {}</code></h5> |
| 162 | + |
| 163 | + <p><strong>Note:</strong> <code>match[0]</code> will be deleted prior to being passed to a filter, and must not be used.</p> |
| 164 | + |
| 165 | + <p>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.</p> |
| 166 | + |
| 167 | + <h4>Attributes</h4> |
| 168 | + |
| 169 | + <h5><code>jQuery.expr.attrHandle.LOWERCASE_NAME = function( elem, casePreservedName, isXML ) {}</code></h5> |
| 170 | + |
| 171 | + <p>Handle an attribute which requires specialized processing (such as <code>href</code>, which has cross-browser issues). The return result must be the actual string value of that attribute.</p> |
| 172 | + |
| 173 | + <h4>Pseudo-selectors (pseudos)</h4> |
| 174 | + |
| 175 | + <h5><code>jQuery.expr.pseudos.NAME = function( elem ) {}</code></h5> |
| 176 | + |
| 177 | + <p><em>The most common extension to a selector engine</em>: adding a new pseudo. The return result from this function must be boolean: true if the element matches the selector, false if not.</p> |
| 178 | + |
| 179 | + <p>For example, this defines a simple <code>:fixed</code> pseudo:</p> |
| 180 | + <pre><code> |
| 181 | +var $test = jQuery( document ); |
| 182 | +jQuery.expr.pseudos.fixed = function( elem ) { |
| 183 | + $test[ 0 ] = elem; |
| 184 | + return $test.css( "position" ) === "fixed"; |
| 185 | +}; |
| 186 | + </code></pre> |
| 187 | + |
| 188 | + <h5><code>jQuery.expr.createPseudo( function )</code></h5> |
| 189 | + |
| 190 | + <p><code>createPseudo</code> is only required if the custom pseudo-selector accepts an argument.</p> |
| 191 | + |
| 192 | + <p><strong>Note</strong>: 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 <code>createPseudo</code> is greatly encouraged.</p> |
| 193 | + |
| 194 | + <p>Now that the parser compiles a single function containing other functions, custom pseudo-selectors with arguments are much cleaner.</p> |
| 195 | + |
| 196 | + <p>For example, within jQuery, the implementation of the <code>:not( <sub-selector> )</code> pseudo is very similar to:</p> |
| 197 | + |
| 198 | + <pre><code> |
| 199 | +jQuery.expr.pseudos.not = |
| 200 | + jQuery.expr.createPseudo( function( subSelector ) { |
| 201 | + var matcher = jQuery.find.compile( subSelector ); |
| 202 | + return function( elem ) { |
| 203 | + return !matcher( elem ); |
| 204 | + }; |
| 205 | + } ); |
| 206 | + </code></pre> |
| 207 | + |
| 208 | + <h5><code>jQuery.expr.setFilters.LOWERCASE_NAME = function( elements, argument, not ) {}</code></h5> |
| 209 | + |
| 210 | + <p>These filters are run after a previous part of a selector has already returned results. <code>setFilters</code> are found from matching <code>jQuery.expr.match.POS</code>. When applicable, <code>argument</code> is expected to be an integer. The <code>not</code> argument is a boolean indicating whether the result should be inverted (as in <code>div:not(:first)</code>).</p> |
| 211 | + |
| 212 | + <p>For example, the code for the <code>:first</code> setFilter is similar to:</p> |
| 213 | + <pre><code> |
| 214 | +var first = function( elements, argument, not ) { |
| 215 | + // No argument for first |
| 216 | + return not ? elements.slice( 1 ) : [ elements[ 0 ] ]; |
| 217 | +}; |
| 218 | +jQuery.expr.setFilters.first = first; |
| 219 | + </code></pre> |
| 220 | + |
| 221 | + <p>It is easy to extend jQuery selection engine — even jQuery's <code>POS</code> selectors. For example, to rename <code>:first</code> as <code>:uno</code>:</p> |
| 222 | + <pre><code><![CDATA[ |
| 223 | +jQuery.expr.match.POS = new RegExp( oldPOS.source.replace( "first", "uno" ), "gi" ); |
| 224 | +jQuery.expr.setFilters.uno = jQuery.expr.setFilters.first; |
| 225 | +delete jQuery.expr.setFilters.first; |
| 226 | +jQuery.find( "div:uno" ); // ==> [ <div> ] |
| 227 | + ]]></code></pre> |
| 228 | + |
| 229 | + <h3 id="jquery-find-internal-api">Internal API</h3> |
| 230 | + |
| 231 | + <p><strong>Note:</strong> Functionality should be accessed via the Public and Extension APIs. While the Internal API is intended specifically for internal use, it has been exposed for edge cases.</p> |
| 232 | + |
| 233 | + <h4><code>jQuery.expr.cacheLength</code></h4> |
| 234 | + |
| 235 | + <p>jQuery internally caches compiled selector functions and tokenization objects. The length of these caches defaults to 50, but can be set to any positive integer by assigning to this property.</p> |
| 236 | + |
| 237 | + <h4><code>jQuery.find.compile( selector )</code></h4> |
| 238 | + |
| 239 | + <p>This method compiles a selector function and caches it for later use. For example, calling <code>jQuery.find.compile( ".myWidget:myPseudo" )</code> during initialization of a plugin will speed up the first selection of matching elements.</p> |
| 240 | + |
| 241 | + <p><strong>returns(Function)</strong>: The compiled function to be used when filtering the set of possibly matching elements</p> |
| 242 | + |
| 243 | + <p><em>Parameters</em></p> |
| 244 | + |
| 245 | + <p><strong>selector</strong>: A CSS selector</p> |
| 246 | + |
| 247 | + </longdesc> |
| 248 | +</entry> |
0 commit comments