我目前正试图找到一个元素的父元素的父元素。我在 <td>
中点击了一个链接,我想得到 <tr>
对象。
为什么“$(this).parent().parent()”不起作用?会怎样?
谢谢,
布伦丹
编辑: 似乎我的语法错误导致了整个过程的失败。 "$(this).parent().parent()"实际上确实有效,但我最终选择了 $(this).closest('tr')"因为它似乎是最有效的解决方案。
最佳答案
最好的方法可能是使用closest
:
$(this).closest('tr');
Closest works by first looking at the current element to see if it matches the specified expression, if so it just returns the element itself. If it doesn't match then it will continue to traverse up the document, parent by parent, until an element is found that matches the specified expression. If no matching element is found then none will be returned.
关于javascript - parent 的 jQuery parent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57404529/