问题
Is there anyway to automatically add a class to any HREF that is set to a hash (#)?
I am using an auto menu in Concrete5 so can't just hard code it in unfortunately.
I'm going to take a guess and say that it will be JavaScript/jQuery that will have to be used?
Thanks in advance!
回答1:
Do like this,
$('a[href="#"]').addClass('className');
Here in the above code we have used attribute equals selector.
回答2:
You may try to use
$('a[href$="#"]').addClass('className');
Check the docs for options of attribute selector:
Attribute Contains Prefix Selector [name|="value"]
Selects elements that have the specified attribute with a value
either equal to a given string or starting with that string followed
by a hyphen (-).
Attribute Contains Selector [name*="value"]
Selects elements that have the specified attribute with a
value containing the a given substring.
Attribute Contains Word Selector [name~="value"]
Selects elements that have the specified attribute with a value
containing a given word, delimited by spaces.
Attribute Ends With Selector [name$="value"]
Selects elements that have the specified attribute with a
value ending exactly with a given string. The comparison is case sensitive.
Attribute Equals Selector [name="value"]
Selects elements that have the specified attribute with a
value exactly equal to a certain value.
Attribute Not Equal Selector [name!="value"]
Select elements that either don’t have the specified attribute,
or do have the specified attribute but not with a certain value.
Attribute Starts With Selector [name^="value"]
Selects elements that have the specified attribute with a
value beginning exactly with a given string.
Has Attribute Selector [name]
Selects elements that have the specified attribute, with any value.
Multiple Attribute Selector [name="value"][name2="value2"]
Matches elements that match all of the specified attribute filters.
回答3:
Try below code its easy
$('a[href*="#"]').addClass('Class');
Demo here JSFiddle
Hope it helps...
来源:https://stackoverflow.com/questions/24674304/add-a-class-to-a-href-thats-set-to