javascript - parent 的 jQuery parent

标签 javascript jquery

我目前正试图找到一个元素的父元素的父元素。我在 <td> 中点击了一个链接,我想得到 <tr>对象。

为什么“$(this).parent().parent()”不起作用?会怎样?

谢谢,
布伦丹

编辑: 似乎我的语法错误导致了整个过程的失败。 "$(this).parent().parent()"实际上确实有效,但我最终选择了 $(this).closest('tr')"因为它似乎是最有效的解决方案。

最佳答案

最好的方法可能是使用closest:

$(this).closest('tr');

Check out the documentation :

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/

相关文章:

javascript - 返回两个对象之间的差异

javascript - 如何在 Javascript 数组中添加 HTML 标记

javascript - Extjs 5.1 图表图例

javascript - json数据不返回值

jquery - 使用 Promise 对象拒绝 $.when ?

javascript - jQuery 在所有事件的 CSS 转换完成后计算宽度和高度

javascript - 从 json 数组中搜索并删除对象

javascript - index.js 没有做 index.ts 做的事情

javascript - react onBlur setState 不起作用(jsbin)

javascript - 为什么我的 javascript 时钟会在一分钟后崩溃?