javascript 如何使用jQuery在html中获取div的祖父母的祖父母

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9150228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:44:51  来源:igfitidea点击:

How to get the grandparent of the grandparent of a div in html using jQuery

javascriptjqueryhtml

提问by Alon

Is there a better way to get the parent of the parent of the parent... like 5 times?

有没有更好的方法来获得父母的父母的父母......比如5次?

So Instead of using this:

所以不要使用这个:

$(this).parent().parent().parent().parent().parent()

I could use something like this:

我可以使用这样的东西:

$(this).goBacktoNthParent(5);

Is it possible?

是否可以?

回答by

Using the :eq()selector

使用:eq()选择器

The :eq()selector allows you to target the element at the zero-based index of the set that was matched...

:eq()选择允许您将元素被匹配集合的从零开始的索引处的目标...

$(this).parents(':eq(4)')

Note: use parent(s) selector

注意:使用父选择器

Because the selector is zero based, we gave it one less than the number targeted.

因为选择器是基于零的,所以我们给它比目标数字小 1。

DEMO:http://jsfiddle.net/pmMFv/

演示:http ://jsfiddle.net/pmMFv/



Using the .eq()method

使用.eq()方法

The .eq()method is effectively the same as its selector counterpart, allowing you to target the element at the zero-based index of the set that was matched...

.eq()方法实际上与其对应的选择器相同,允许您将元素定位在匹配集合的从零开始的索引处...

$(this).parents().eq(4)

Note: use parent(s) selector

注意:使用父选择器

DEMO:http://jsfiddle.net/pmMFv/2/

演示:http ://jsfiddle.net/pmMFv/2/



Using parentNodein a loop

使用parentNode一个循环

For greatest performance, we could use the .parentNodeproperty of the element, and run it in a loop, updating a variable with the newest parent each time...

为了获得最佳性能,我们可以使用.parentNode元素的属性,并在循环中运行它,每次使用最新的父元素更新一个变量......

var el = this, i = 5;
while(i-- && (el = el.parentNode));

$(el)

DEMO:http://jsfiddle.net/pmMFv/1/

演示:http ://jsfiddle.net/pmMFv/1/

回答by Niet the Dark Absol

You can easily create your own function:

您可以轻松创建自己的函数:

function getNthParentOf(elem,i) {
    while(i>0) {
        elem = elem.parent();
        i--;
    }
    return elem;
}

var something = getNthParentOf($(this),5);

回答by Maxim Manco

You can use the .parentstraversing function in conjunction with the :nth()selector.

您可以将.parents遍历函数与:nth()选择器结合使用。

So the result will be something like:

所以结果将是这样的:

$(this).parents(':nth(5)'));

Notice:the :nth()index starts from 0 so for your case it should be:

注意::nth()指数从0开始所以你的情况应该是:

$(this).parents(':nth(4)'));

回答by Rory McCrossan

If there are identifying markers on the parent element you want to get - such as an idor classyou can use $(this).closest("#grandparentElement")

如果您想要获取的父元素上有识别标记 - 例如 an idorclass您可以使用$(this).closest("#grandparentElement")

回答by Lokesh Yadav

Hope, this would be of any help.

希望,这会有所帮助。

try using .parentsUntil()

尝试使用 .parentsUntil()

working example: http://jsfiddle.net/ylokesh/wLhcA/

工作示例:http: //jsfiddle.net/ylokesh/wLhcA/

回答by Ananda Sudarshan

Well you can try

那么你可以试试

var parentControl = $('#yourcontrol').parent();
var grandParent = parentControl.parent();