将类添加到设置为'#'的href中。

时间:2014-07-10 10:33:30

标签: jquery html5 url href

是否有自动将类添加到任何设置为哈希(#)的HREF?

我在Concrete5中使用了一个自动菜单,所以不幸的是它不能硬编码。

我会猜测并说它将是必须使用的JavaScript / jQuery?

提前致谢!

3 个答案:

答案 0 :(得分:3)

这样做,

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

在上面的代码中,我们使用了attribute equals selector

答案 1 :(得分:1)

您可以尝试使用

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

选中docs以查看属性选择器的选项:

    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.

答案 2 :(得分:0)

尝试以下代码轻松

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

在这里演示JSFiddle

希望它有所帮助...