开发者

Select anchors without href

开发者 https://www.devze.com 2023-04-04 11:17 出处:网络
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

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/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号