【问题标题】:Identify element using multiple classes in random arrangement [duplicate]使用随机排列的多个类识别元素[重复]
【发布时间】:2018-11-29 10:39:27
【问题描述】:

有没有办法在不知道类排列的情况下使用它的类来获取元素?

我这里有一个示例 HTML

<table>
  <tr class="header first test1">
    <th>Month</th>
    <th>Savings</th>
  </tr>
  <tr class="header first test2">
    <td>January</td>
    <td>$100</td>
  </tr>
  <tr class="h34der third test1">
    <td>February</td>
    <td>$80</td>
  </tr>
</table>

我需要获取具有 headertest1 作为其类的元素

我做的选项如下:

选项#1

$('.header, .test1').css('background','yellow');

但是它是不正确的,因为它为所有具有“header”或“test1”类的元素着色。

选项 #2 我看到这是可能的

$("[class*='header first test1']").css('background','yellow');

但我的情况是我不知道“first”类,我知道的唯一类是“header”和“test1”。

有没有办法使用多个类来识别元素? 感谢您在这方面的帮助。

这是我用于测试的小提琴:http://jsfiddle.net/ky8d94n6/

【问题讨论】:

  • $('.header.test1')
  • 在发布问题之前做一些研究。

标签: javascript jquery html css


【解决方案1】:

使用$('.header.test1') 选择元素将类headertest1

$(document).ready(function(){
    $('.header.test1').css('background','yellow');
});

$(document).ready(function(){
	$('.header.test1').css('background','yellow');
});
table, th, td {
    border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr class="header first test1">
    <th>Month</th>
    <th>Savings</th>
  </tr>
  <tr class="header first test2">
    <td>January</td>
    <td>$100</td>
  </tr>
  <tr class="h34der third test1">
    <td>February</td>
    <td>$80</td>
  </tr>
</table>

【讨论】:

    猜你喜欢
    • 2021-07-10
    • 1970-01-01
    • 2012-01-12
    • 1970-01-01
    • 2012-07-28
    • 2012-02-07
    • 2014-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多