[英]How to find the deepest ul/ol with list items in a nested list
I am trying to clean up bulleted lists generated by another program which has created nested lists which are unnecessary.我正在尝试清理由另一个程序生成的项目符号列表,该程序创建了不必要的嵌套列表。 I need to remove them.
我需要删除它们。 Here are 2 examples...
这里有2个例子...
<!DOCTYPE html> <html lang="en"> <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>
I need to find the ul/ol with valid list item context (any text) and remove all the extraneous uls wrapped around them.我需要找到具有有效列表项上下文(任何文本)的 ul/ol 并删除所有围绕它们的无关 ul。 To do this, I was trying to find the deepest ul that has list items.
为此,我试图找到具有列表项的最深 ul。 I tried a few selector like
$("ul:has(li)")
, but this returns all the parent uls also.我尝试了一些选择器,如
$("ul:has(li)")
,但这也会返回所有父级$("ul:has(li)")
。
Try this:尝试这个:
$('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> <ul> <li> <ul> <li>List Item 1</li> <li>List Item 2</li> </ul> </li> </ul> </ul> <ul> <li> <ul> <li> <ul> <li> <ol> <li>Another LI 1</li> <li>Another LI 2</li> <li>Another LI 3</li> </ol> </li> </ul> </li> </ul> </li> </ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.