JS/jQuery: Get depth of element?

后端 未结 6 2130
面向向阳花
面向向阳花 2020-12-29 07:21

What\'s the easiest way to get the depth of an element in pure JavaScript or jQuery? By \"depth\" I mean how many elements deep is it nested, or how many ancestors does it h

6条回答
  •  孤独总比滥情好
    2020-12-29 07:35

    An additional note. If you want to get the depth relative to a certain context you can do:

    var depth = $("#my-element","#ContextContainerID").parents("ul").length;
    

    Above, I'm searching for how many UL's are within the container #ContextContainerID

提交回复
热议问题