如何在嵌套列表中使用列表项找到最深的 ul/ol

我正在尝试清理另一个程序生成的项目符号列表,该程序创建了不必要的嵌套列表。我需要删除它们。这里有2个例子......


<!DOCTYPE html>

<html>


<head>

  <title>Document</title>

</head>


<body>

  <ul>

    <ul>

      <li>

        <ul>

          <li>List Item 1</li>

          <li>List Item 2</li>

        </ul>

      </li>

    </ul>

  </ul>

   <ul>

  <li>

    <ul>

      <li>

        <ul>

          <li>

            <ol>

              Prep Steps

              <li>Step 1</li>

              <li>Step 2</li>

              <li>Step 3</li>

            </ol>

            <ul>

              Other things to note

              <li>Another LI 1</li>

              <li>Another LI 2</li>

              <li>Another LI 3</li>

            </ul>

          </li>

        </ul>

      </li>

    </ul>

  </li>

</ul>

 <script src="https://code.jquery.com/jquery-3.5.0.js"></script>

</body>


</html>


我需要找到具有有效列表项上下文(任何文本)的 ul/ol 并删除围绕它们的所有无关的 ul。为此,我试图找到具有列表项的最深的 ul。我尝试了一些类似的选择器$("ul:has(li)"),但这也返回了所有父 uls。


喵喵时光机
浏览 120回答 1
1回答

狐的传说

尝试这个:&nbsp;&nbsp;&nbsp;&nbsp;$('ul,ol').not(':has(ul,ol)')$('ul,ol').not(':has(ul,ol)').each((index,list) => console.log(list));<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><ul>&nbsp; &nbsp; <ul>&nbsp; &nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp; &nbsp; <ul>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>List Item 1</li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>List Item 2</li>&nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; </ul>&nbsp; </ul>&nbsp; <ul>&nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp; <ul>&nbsp; &nbsp; &nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ul>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ol>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>Another LI 1</li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>Another LI 2</li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>Another LI 3</li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ol>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; </li>&nbsp; </ul>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript