How to find nth parent of an element using jquery

前端 未结 4 1918
忘掉有多难
忘掉有多难 2020-12-24 10:43

I want to find the nth parent element of an given element and access the attributes of parent.


4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-24 11:26

    You could make a little plugin to take care of that:

    $.fn.nthParent = function(n){
        var p = this;
        for(var i=0;i

    and then use it as:

    $('#element1').nthParent(3);
    

提交回复
热议问题