jQuery filter :contains for a link (a href="#")
I have a link that is currently:
a href="#"
but soon the client will change the link to
a href="something"
When the link becomes somethi开发者_Python百科ng I would like to use jQuery to change the css, but I am not sure how to write a filter for an attribute (href="")...
Here are two options that you can use to compare your href
attribute:
Href is empty:
$('a[href=""]') or $('a[href="#"]')
Href isn't empty:
$('a[href!=""]') or $('a[href!="#"]')
When comparing attributes, jQuery offers several operators for comparisions:
= Is Equal
!= Is Not Equal
^= Starts With
$= Ends With
*= Contains
More on jQuery Selectors here.
Here's a selector for an anchor with an href not equal to hash:
$('a[href!="#"]')
A quick read on jQuery selectors should do the trick (as mentioned by Rionmonster above). I'd just like to note that you can also check for elements with a specific attribute (which I expect you'll actually need?) by doing something like
$('a[href]')
This finds <a>
elements that have a defined href
attribute, whether that be a hash or anything else. Good for filtering out clickable anchors.
Read about JQuery selectors:
http://api.jquery.com/category/selectors/
精彩评论