JS/jQuery: Get depth of element?

后端 未结 6 2086
面向向阳花
面向向阳花 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条回答
  •  萌比男神i
    2020-12-29 07:52

    Supposing you don't want to include body and html tag in the parents to count use:

    $("#element").parents("*").not("body,html").size()
    

    Online demo here: http://jsfiddle.net/zaJff/

提交回复
热议问题