Ric*_*ema 5 jquery jquery-selectors
我有一个有6列的表.我想在每行的最后3列(不包括标题行)上运行一个函数.我有以下选择器正在工作:
$('table.listviewtable > tbody > tr:gt(0)').find('td:gt(2)')
.each(function () { runMyFunction($(this)); });
Run Code Online (Sandbox Code Playgroud)
有没有办法在一个选择器中完成所有操作(即没有中间件find)?
更新:
我试过了
$('table.listviewtable > tbody > tr:gt(0) td:gt(2)')
Run Code Online (Sandbox Code Playgroud)
和
$('table.listviewtable > tbody > tr:gt(0) > td:gt(2)')
Run Code Online (Sandbox Code Playgroud)
但他们没有工作.他们返回第2行的第4,第5和第6列以及所有后续行的所有列.
干得好:
$('table.listviewtable td:nth-child(n+4)')
Run Code Online (Sandbox Code Playgroud)
现场演示: http ://jsfiddle.net/simevidas/qJ3tP/1/
(我假设您正在为标题单元格使用TH元素.)
获得TR的X最后TD的通用规则是:
$('#tbl tr td:nth-last-child(-n+X)');
Run Code Online (Sandbox Code Playgroud)
// Example for the last 3 paragraphs in some section:
$('#my_section p:nth-last-child(-n+3)');
// It's exactly the same as:
$('#my_section p:nth-last-child(3-n)');
Run Code Online (Sandbox Code Playgroud)
<!-- CSS -->
<style>
td {
border: solid 1px #999;
padding: 2px;
}
</style>
<!-- HTML -->
<p>Last 3 cells made gray using jQuery</p>
<table>
<tr>
<td>One</td>
<td>Two</td>
<td>Three</td>
<td>Four</td>
</tr>
</table>
<!-- JavaScript -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('table tr td:nth-last-child(-n+3)').css('background', '#CCC');
});
</script>Run Code Online (Sandbox Code Playgroud)
http://css-tricks.com/useful-nth-child-recipies/
| 归档时间: |
|
| 查看次数: |
12680 次 |
| 最近记录: |