Select anchors without href
I'd like to chan开发者_开发技巧ge the css style of all the anchors without href
attribute.
I can use selector to select anchors that start with something using $('a[href^="some_link"]')
. But I want it to be vice versa. How can I select anchors without href using jquery?
Also, can I achieve this using a css selector?
Thank you.
You'll want this selector (jQuery/CSS compatible):
a:not([href])
If you need IE support (because of that :not()
), use it as a jQuery selector. Otherwise, you can use it for a CSS rule.
Use $("a").filter(function() { return !this.attributes['href']; })
.
It's a little longer, but browsers can optimise the selection of the anchors, and do the filtering faster than Sizzle (or querySelectorAll) can run.
http://jsfiddle.net/9PWQB/
精彩评论