如何获取td's的tr,其中td指数(行内)不为1或2?也就是说,我想跳过前两列.以下代码完成了工作,但是有更好的方法吗?我在想(td eq(0) || eq(1))这样的事情.
$("#time-entry tr").each(function () {
var trId = $(this).attr('id');
$(String.format("#{0} td input[id^=txt]", trId)).each(function () { //rewrite
var tdIndex = $(this).index();
if(tdIndex != 1 && tdIndex != 2) {
}
});
});
Run Code Online (Sandbox Code Playgroud)
假设'index'是其父tr中该td的索引
$("#time-entry tr td").not(":nth-child(1), :nth-child(2)");
Run Code Online (Sandbox Code Playgroud)
编辑: - 这个更短
$("#time-entry td:not(:nth-child(1), :nth-child(2))");
Run Code Online (Sandbox Code Playgroud)
编辑2: -
关于通过.each()函数传递的索引:
选择器(例如$("td"))将获得一个元素列表,它传递的索引是它在该列表中的索引.
编辑3: -询问:gt()
它查找匹配集的索引.因此,如果匹配的集合是<td>表格中的所有元素,那么它将不适合您.它会以这种方式工作:
$("#time-entry tr").each(function () {
$("td:gt(1)", this).each(function(){
});
});
Run Code Online (Sandbox Code Playgroud)
但我不知道为什么人们会赞成这种方式超过其他建议的方式.
| 归档时间: |
|
| 查看次数: |
11448 次 |
| 最近记录: |