views:

33

answers:

1

It seems like the selector ":link" is not supported by jQuery's filter() or is() function. For instance, if I evaluate $(":link") on a page it returns multiple links. If I evaluate $(":link").filter(":link") or $(":link").is(":link"), an error is thrown. The error message is "Syntax error, unrecognized expression: link".

Is this by design? Do filter() and is() not support the same CSS selectors is jQuery does generally? Is there documentation as to the difference?

+4  A: 

Patrick commented that jQuery defaults in some cases to the browser's built in querySelector or querySelectorAll where it exists. So, :link appears to work on some browsers, but I wouldn't suggest using it as it seems to produce wonderfully bizarre results.

<a href="www.foo.com">Hello</a>

alert($("a").is("a")); // do it this way

// changes the anchor's CSS, but does not return the length
alert($(":link").css('background','yellow').length); 

Demo: ​http://jsfiddle.net/xWPw7/4

karim79
Your third test doesn't actually fail (in Safari and Firefox anyway). The second test crashes so the third alert never happens. If you remove the second test so the third can run, Safari shows a length of 1, while FF shows 3. And CSS is properly applied to the link with `.css()`. Very odd. http://jsfiddle.net/xWPw7/3/
patrick dw
...I know what it is. jQuery defaults in some cases to the browswer's built in `querySelector` or `querySelectorAll` when it exists. Apparently `:link` is supported. http://jsfiddle.net/xWPw7/4/
patrick dw
@Patrick - nice one. This is very odd indeed.
karim79
@Patrick - you should probably post that as an answer, and I should delete this post.
karim79
You go ahead. I should get back to what I'm working on. :o)
patrick dw
@patrick - done :)
karim79
Thanks everyone for chipping in.
Frank Schwieterman