【问题标题】:How to get specific class attribute [duplicate]如何获取特定的类属性[重复]
【发布时间】:2014-06-16 08:43:56
【问题描述】:
<a class="ab ac ad">HOME</a>

我需要第二个类属性,像这样:

$("a").click(function(){
var a = $(this).attr("class").eq(1);
alert (a);
});

有什么想法吗?

【问题讨论】:

  • 对于大多数用例,您应该改用$(selector).hasClass(className)
  • this.className.split[1]
  • @Mr_Green,您的解决方案不起作用

标签: jquery html


【解决方案1】:

你得到一个字符串,所以你可以把它拆分:

var a = $(this).attr("class").split(' ')[1];

【讨论】:

  • 为什么投反对票?如果你不解释你认为错的地方是什么,它就无法改进答案。
【解决方案2】:

你可以使用空格分割:

echo $(this).attr("class").split(' ')[1];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-12
    • 1970-01-01
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    • 2022-10-04
    • 2019-04-29
    相关资源
    最近更新 更多