JS/jQuery: Get depth of element?

后端 未结 6 2095
面向向阳花
面向向阳花 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:33

    My advice would be to rethink the way you are solving your problem - I think finding the number of generations between nodes probably isn't the best way to go, it sounds like a solution that will be likely to break easily by future changes to the code.

    If you insist however, the solution (by Cletus, in native javascript) seems pretty good on this page: find number of nodes between two elements with jquery?

提交回复
热议问题