父母的jQuery父级 [英] jQuery parent of a parent

查看:95
本文介绍了父母的jQuery父级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试查找元素父级的父级。我点击的链接位于< td> 中,我想获得< tr> object。

I am currently trying to find the parent of a parent of an element. I have a link being clicked that is in a <td>, and I'd like to get the <tr> object.

为什么不会$(this).parent()。parent()工作?什么会?

Why wont "$(this).parent().parent()" work? What will?

谢谢,

Brendan

Thanks,
Brendan

编辑:在我的语法中出现错误是抛弃了整个事情。 $(this).parent()。parent()确实可以工作,但我最后还是花了$(this).closest('tr'),因为它似乎是最有效的解决方案。

It appears an error in my syntax was throwing the whole thing off. "$(this).parent().parent()" does in fact work, but I wound up going with $(this).closest('tr')" because it seems like the most efficient solution.

推荐答案

最好的方法可能是使用最近的

The best way would probably be using 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.

这篇关于父母的jQuery父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆