javascript - Find depth of each children of body in an HTML document -


in below code sample, body has 2 children. know max depth of each child body element has 2 children nodes, how find each child's max depth. here in case: child 1 depth: 3 child 2 depth: 5

<body>      <ul>          <li>list 1</li>          <ul>              <li>                  <h1>heading 1</h1>              </li>          </ul>      </ul>      <ul>          <li>              <ul>                  <li>                      <ul>                          <li></li>                      </ul>                  </li>              </ul>          </li>      </ul>      </body>

if use jquery quite straight forward although question isn't clear.

$('.child-2').parents().length 

or if use calcdepth question isn't clear.

var calcdepth = function ( root ) {     var $children = $( root ).children();     var depth = 0;      while ( $children.length > 0 ) {         $children = $children.children();         depth += 1;     }      return depth; }; var result = calcdepth( document.getelementbyid('parent-1') ); 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -