我在 html 表格的每一行的表格单元格中都有一个链接( anchor )标记。 单击此 anchor 标记后,我想使用 jquery(最好)遍历回父 td 和 tr 并获取对其的对象引用。
我如何在这里最好地使用 jquery 在 dom 中导航\遍历。
我可以做这样的方法,但不确定 jquery 是否有更好的方法。
function findRowNumber(element)
{
// element is a descendent of a tr element
while(element.tagName.toLowerCase() != "tr")
{
element = element.parentNode; // breaks if no "tr" in path to root
}
return element.rowIndex;
}
最佳答案
这正是“closest ”的用途。
$(this).closest('td');
$(this).closest('tr');
关于jquery - 从单元格中单击的项目遍历到父 td 和 tr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/921094/