【发布时间】: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>
我需要获取具有 header 和 test1 作为其类的元素
我做的选项如下:
选项#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