parent()中的jQuery边缘情况测试

时间:2016-01-08 01:12:39

标签: jquery html

刚刚为雇主测试做了一些示例代码,由于边缘情况我没有通过:(。知道这些边缘情况可能是什么?问题如下(简化): 给定一个示例HTML文档,在警报中显示ul / ol对象的最深嵌套。

例如,以下代码具有4层的ul / ol对象嵌套。

我无法想到任何可能的边缘情况。他们会是什么?错误检查有点不适用,因为它只查找匹配..

的父节点

有人能想到这个代码不起作用的HTML案例吗?只是快速描述可能导致问题的HTML会很棒。

function solution() {
    //This value will be returned
   var listDepth=0;
   //Loop through each ul (jQuery)
   $('ul').each(function(){
       var depth= $(this).parents("ul,ol").length; //find the number of ul,ol elements parented
       if (depth>listDepth)
       {
           listDepth=depth+1; //+1 includes root
       }
   });
   //Loop through each ol (jQuery)
   $('ol').each(function(){
       var depth= $(this).parents("ul,ol").length; //find the number of ul or ol elements above
       if (depth>listDepth)
       {
           listDepth=depth+1; //+1 includes root
       }
   });
  window.alert(listDepth);
   return listDepth; //return the list with most depth
  
}
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
  <body>
  
  <ul>
    <li>
      <ul>
      <li>
        <ol>
          <li>
          </li>
        </ol>
      </li>
      </ul>
    </li>
    <li>
       <ul>
      <li>
        <ol>
          <li>
            <ol>
              <li>
                </li>
             </ol>
          </li>
        </ol>
      </li>
      </ul>
    </li>
   </ul>
  
  <button type=button onclick=solution()>Try Me </button>
  
  </body>
  </html>

0 个答案:

没有答案
相关问题