你好,我想像这样循环遍历一些 tr。
<tr>
<td> <input type="radio" id="r11" value="a" name="r1_selectedobjects" /> </td>
<td><input type="radio" id="r12" value="b" name="r1_selectedobjects" /> <td>
</tr>
<tr>
<td><input type="radio" id="r21" value="a" name="r2_selectedobjects" /></td>
<input type="radio" id="r22" value="b" name="r2_selectedobjects" /></td>
</tr>
<td><input type="radio" id="r31" value="a" name="r3_selectedobjects" /></td>
<td><input type="radio" id="r32" value="b" name="r3_selectedobjects" /></td>
<tr>
<tr>
<td><input type="submit" id="matbutton" data-inline="true" value="Submit" onclick="return CheckMatrixRadio(this);" /></td>
</tr>
我可以通过 $('tr').each(function(){});
我想跳过 3 rd tr
这个
</tr>
<td><input type="radio" id="r31" value="a" name="r3_selectedobjects" /></td>
<td><input type="radio" id="r32" value="b" name="r3_selectedobjects" /></td>
<tr>
如何做到这一点。认为我不知道任何 calss 或 id 名称,我唯一知道的是我的 tr 的索引,在这种情况下,3
如何在我的 each
循环中跳过那个 tr
。请帮助………………
最佳答案
each
回调收到一个从零开始的索引,告诉您正在查看哪个匹配项,因此:
$('tr').each(function(index){
// If not the third one...
if (index !== 2) {
// ...do something
}
});
如果出于任何原因你不想使用 each
(你在你的问题中提到了它,但只是覆盖了基础)而是想创建一个包含除第三行之外的所有行的 jQuery 实例一,我知道的最有效的方法是:
var rows = $('tr');
rows = rows.not(rows[2]);
...通过 not
从匹配集中删除第三行.您也可以使用涉及 :not
的选择器实现相同的目的。和 :eq
:
$('tr:not(:eq(2))')
...但是您要求 jQuery 处理选择器,而不是允许它将选择器传递给浏览器的 querySelectorAll
实现(如果它有一个)。 *
(旁注:如果页面上的任何地方都有不相关的 tr
,您可能需要让您的选择器更具体地针对您正在查看的行组。)
* PiTheNumber 指出his answer $('tr:not(:nth-child(3))')
也应该这样做。这很有用,因为它具有 :not
和 :nth-child
都可以由浏览器本地处理的优势。请注意,:nth-child
使用基于 1 的索引,并且 非常 与 :eq
不同——它检查元素是其哪个子元素容器,而不是它是否是第 n 个 matched 元素。不过,应该适用于包含 tr
元素的表格,前提是它们都在同一个 tbody
中。
关于javascript - 遍历一组 tr 并通过 jquery 中的索引转义特定的 tr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8984497/