获取所有父母信息直到指定的父母

时间:2020-12-02 16:47:11

I have this html codes example:

我有这个HTML代码示例:

<html>
    <body>
        <div>
            <div id="stophere">
               <h4 class="parentclass"> 
                <span class="target">Clicked</span>
               </h4>
            </div>
        </div>
    </body>
</html>

From the html codes example above, I want to get all parents' tag name of class target (when receiving click event) down from div with id stophere.

从上面的html代码示例中,我想从id为stop的div获取所有父类的类目标标记名(当接收单击事件时)。

I tried this code:

我试过这段代码:

$(ev.target).parents()
            .map(function() {
            return this.tagName;
            })
            .get()
            .join( ", " );

But it includes all parents' tag names above stophere. While the result I want is only 1 div and 1 h4.

但它包括所有父母的标签名称。虽然我想要的结果只有1 div和1 h4。

What is the correct way to get all parents of target down from stophere?

让目标的所有父母远离荒芜的正确方法是什么?

2 个解决方案

#1


4  

You can use the parentsUntil method for that

您可以使用parentsUntil方法

$(ev.target).parentsUntil($('#stophere').parent())

Note that it's non-inclusive, so we pass the parent of #stophere to include that element as well

请注意,它是非包含性的,因此我们传递#stophere的父级以包含该元素

FIDDLE

小提琴

#2


0  

I don't claim that this is a good solution, but can be used if adeneo's solution is failed in your situation like in my case.

我并不认为这是一个很好的解决方案,但如果adeneo的解决方案在您的情况下失败,可以使用,就像我的情况一样。

This code is checking whether the traversing limit is containing that limit line itself or not, by using find() method:

此代码通过使用find()方法检查遍历限制是否包含该限制行本身:

    jQuery('html').on("click", function (ev) {
        var elemparentid = jQuery(ev.target).closest("[id]").attr("id");
        var thisparents = jQuery(ev.target).parents()
            .map(function () {
// check if traversing limit is a children of current element or not, by using find() method
            if (jQuery(this).find("#" + elemparentid).length < 1) {
                return this.tagName;
            }
        }).get()
            .join(", ");
        alert(thisparents);
    });

FIDDLE

小提琴

#1


4  

You can use the parentsUntil method for that

您可以使用parentsUntil方法

$(ev.target).parentsUntil($('#stophere').parent())

Note that it's non-inclusive, so we pass the parent of #stophere to include that element as well

请注意,它是非包含性的,因此我们传递#stophere的父级以包含该元素

FIDDLE

小提琴

#2


0  

I don't claim that this is a good solution, but can be used if adeneo's solution is failed in your situation like in my case.

我并不认为这是一个很好的解决方案,但如果adeneo的解决方案在您的情况下失败,可以使用,就像我的情况一样。

This code is checking whether the traversing limit is containing that limit line itself or not, by using find() method:

此代码通过使用find()方法检查遍历限制是否包含该限制行本身:

    jQuery('html').on("click", function (ev) {
        var elemparentid = jQuery(ev.target).closest("[id]").attr("id");
        var thisparents = jQuery(ev.target).parents()
            .map(function () {
// check if traversing limit is a children of current element or not, by using find() method
            if (jQuery(this).find("#" + elemparentid).length < 1) {
                return this.tagName;
            }
        }).get()
            .join(", ");
        alert(thisparents);
    });

FIDDLE

小提琴