Add a class to a href that's set to '#'

2019-09-19 15:56发布

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!

3条回答
Evening l夕情丶
2楼-- · 2019-09-19 16:30

Do like this,

$('a[href="#"]').addClass('className');

Here in the above code we have used attribute equals selector.

查看更多
来,给爷笑一个
3楼-- · 2019-09-19 16:45

Try below code its easy

$('a[href*="#"]').addClass('Class');

Demo here JSFiddle

Hope it helps...

查看更多
太酷不给撩
4楼-- · 2019-09-19 16:55

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.
查看更多
登录 后发表回答