Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy do functional pseudos such as :not() and :has() allow quoted arguments?
    primarykey
    data
    text
    <p>Apparently, as I've discovered while commenting on <a href="https://stackoverflow.com/questions/12472110/jquery-get-the-last-tr-with-a-th/12472203#12472203">another answer</a>, jQuery (rather its underlying selector engine <a href="http://sizzlejs.com" rel="nofollow noreferrer">Sizzle</a>) lets you quote the argument to the <code>:not()</code> selector as well as the <code>:has()</code> selector. <a href="http://jsfiddle.net/BoltClock/bRE9u" rel="nofollow noreferrer">To wit</a>:</p> <pre><code>$('div:not("span")') $('span:has("span")') </code></pre> <p>In the <a href="http://www.w3.org/TR/selectors" rel="nofollow noreferrer">Selectors standard</a>, quotes are always representative of a string and never of a selector or a keyword, so quoting the argument to <code>:not()</code> is always invalid. <a href="http://www.w3.org/TR/selectors4/#negation" rel="nofollow noreferrer">This will not change in Selectors 4.</a></p> <p>You can also see that it's non-standard syntax by adding an <a href="https://stackoverflow.com/questions/11745274/what-css3-selectors-does-jquery-really-support-e-g-nth-last-child">unsupported CSS selector</a> such as <code>:nth-last-child(1)</code> <a href="http://jsfiddle.net/BoltClock/bRE9u/1" rel="nofollow noreferrer">causing the selector to fail completely</a>:</p> <pre><code>$('div:not("span"):nth-last-child(1)') $('span:has("span"):nth-last-child(1)') </code></pre> <p>Is there any good reason, technical or otherwise, for allowing quotes here? The only possibilities that come to mind are:</p> <ul> <li><p>Consistency with <code>:contains()</code> which allows both quoted and unquoted arguments, as seen in <a href="http://www.w3.org/TR/2001/CR-css3-selectors-20011113/#content-selectors" rel="nofollow noreferrer">the old Selectors spec</a>. Except <code>:contains()</code> accepts strings/keywords, not selectors...</p></li> <li><p>Consistency with the implementation of custom pseudos using <code>$.expr[':']</code>, which always allows quoted and unquoted arguments.</p></li> <li><p>Consistency and ease of porting to their method counterparts <code>.not()</code> and <code>.has()</code> (just remove or split the outer quotes and change colons to periods?).</p></li> </ul> <p>But I can't find any sources to support or oppose them. In fact, the ability to quote selector arguments itself isn't documented anywhere either, nor does there appear to be any difference between quoting and not quoting the argument:</p> <pre><code>$('div:not(span)') $('span:has(span)') </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload