有人可以看一下我的演示并告诉我为什么我的单元格没有突出显示吗?
$("#tbl td.rank").filter(function () {
return $(this).text() > 30;
}).addClass('bright');
http://jsfiddle.net/qmftvxyd/6/
我基本上想突出显示排名> 30的单元格
更新:更改了演示链接,因为错误地提到了类
最佳答案
你有一些错误:
1)您的选择器是错误的。您可以添加 tr
(可选),并且类 .rank
中没有 td
2) 要比较每个 td
的文本,您必须首先使用 parseInt 转换为数字。 .
$("#tbl tr td.age").filter(function() {
return parseInt($(this).text(), 10) > 30;
}).addClass('bright');
.bright {
color: red !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="tbl">
<thead>
<tr>
<th class="id">EmpNo</th>
<th class="name">First Name</th>
<th class="rank">Rank</th>
</tr>
</thead>
<tbody>
<tr>
<td class="id">E342</td>
<td class="name">Bill</td>
<td class="age">35</td>
</tr>
<tr>
<td class="id">E343</td>
<td class="name">Laura</td>
<td class="age">26</td>
</tr>
</tbody>
</table>
关于jquery - 表上奇怪的 filter() 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26838460/