简体   繁体   English

更清洁的方式来编写element.parent()。parent()。parent()。parent()。parent()

[英]Cleaner way to write element.parent().parent().parent().parent().parent()

如果我需要选择第10个父级,是否有更简洁的方法,然后重复.parent()10次?

$('#element_id').parent().parent().parent().parent().parent().parent().parent().parent().parent().parent();

If there's a selector that represents the target you're after, then use .closest() or .parents() . 如果有一个表示你所追求的目标的选择器,那么使用.closest().parents()

$('#element_id').closest('.someClass');
$('#element_id').parents('.someClass:first');

...but both of these will return the first match found. ...但这两个都将返回找到的第一个匹配。 The proper solution will depend on your actual HTML markup. 正确的解决方案取决于您的实际HTML标记。

(Note that .closest() also evaluates the original element, while parents() starts with the first ancestor.) (注意.closest()也会计算原始元素,而parents() .closest()第一个祖先开始。)

Also keep in mind that browsers make HTML corrections. 另请注意,浏览器会进行HTML更正。 So if you're traversing from inside a <table> that has no <tbody> to an element outside the <table> , doing x number of .parent() may give different results in different browsers. 所以,如果你从里面穿过<table>有没有<tbody>到外部的元素<table>这样做的x个.parent()可能会在不同的浏览器不同的结果。

The following post here uses this implementation: 以下职位在这里使用这个实现:

jQuery.fn.getParent = function(num) {
    var last = this[0];
    for (var i = 0; i < num; i++) {
        if(!last) break; 
        last = last.parentNode;
    }
    return jQuery(last);
};
// usage:
$('#myElement').getParent(3);

so your usage would simply be: 所以你的用法只是:

$('#element_id').getParent(10);

如果你确实需要获得第10个父级,并且你无法使用选择器来实现目标,那么最平滑的方式可能是这样的:

$('#element_id').parents().eq(9);

I've used this code: 我用过这段代码:

var position = $("#test").parents("div").length - 10;
$("#test").closest("div:eq(" + position + ")").append("here!!!");

with that HTML : 用那个HTML:

    <div>
        <div>
            <div>
                <div>
                    <div>
                        <div>
                            <div>
                                <div>
                                    <div>
                                        <div>
                                            <div>
                                                <span id="test">here</span>
                                            </div>
                                        </div>      
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

Yes that is 11 div. 是的,这是11 div。 so running the code should stop at the 10th div and append here!!! 所以运行代码应停在第10个div并附加在这里!!!

I'm sure this code could be even more clean. 我确信这段代码可以更干净。

No need to add class. 无需添加课程。

Edit: I used 11 DIV so you can see it's not going to the very first one, it actually stop at the 10th. 编辑:我使用了11 DIV,所以你可以看到它不是第一个,它实际上是在10日停止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM