【问题标题】:jQuery - select one of the two classesjQuery - 选择两个类之一
【发布时间】:2011-03-11 00:41:53
【问题描述】:

我有一个表格行元素:

... <tr index="1000" class="class1 classHighlightRed"> ...
... <tr index="1000" class="class1 classHighlightYellow"> ...

我想根据一些常量检查第二类的值。

例如

    If (2nd class == "classHighlightRed") Then
    {
      doSomeWork;    
     }

目前我正在通过以下 hack 实现这一点:

var 2ndClass = $(this).attr("class").substring(7);

有没有更好的方法来实现这一点?

【问题讨论】:

    标签: javascript jquery jquery-selectors css-selectors


    【解决方案1】:
    var 2ndClass = $(this).attr("class").split(' ')[1];
    

    【讨论】:

      【解决方案2】:

      我相信您可以为此使用 hasClass 方法?

      if($(this).hasClass('classHighlightRed')) Then 
      {
      doWork;
      }
      

      更多信息here

      HTH

      【讨论】:

      • 谢谢,这是更好的选择,因为我不是字符串操作的忠实粉丝。
      猜你喜欢
      • 2023-03-19
      • 2015-08-22
      • 2016-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多