简体   繁体   English

将“ul”元素移动到前一个相邻的“li”元素中

[英]Move 'ul' element into the previous adjacent 'li'` element

I have a poorly formatted sidebar aside which I have no control over the structure.我有一个格式很差的侧边aside ,我无法控制结构。 It automatically generates a ul from the body of the document, but incorrectly is placing the sub-lists outside of the respective parent element.它会自动从文档正文生成ul ,但错误地将子列表放置在相应的父元素之外。

Valid html code should be along the lines of:有效的 html 代码应如下所示:

<ul>
 <li>
  <a>Text</a>
  <ul>
   <li>
    <a>Text</a>
   </li>
  </ul>
 </li>
</ul>

Where the child ul elements are nested inside the previous li element.ul元素嵌套在前一个li元素中。

In the system I am using there is also the ability to have chapter headings, which should be the top level li elements in the main list在我使用的系统中,还有章节标题的功能,它应该是主列表中的顶级li元素

<ul>
 <li><a>Text</a></li>  <-- chapter title
 <li><a>Text</a></li>  <-- chapter title
 <li><a>Text</a>       <-- chapter title
  <ul>
   <li>
    <a>Text</a>        <-- sub-chapter title
   </li>
  </ul>
 </li>
</ul>

Below is a test html structure and some of the javascript I've been trying to get to work.下面是一个测试 html 结构和一些我一直在努力工作的 javascript。

At the moment it is currently iterating through the loop of all the found li elements and adding the next ul element into it.目前它正在遍历所有找到的li元素的循环并将下一个ul元素添加到其中。 However, because it is doing it top down its running into a continual loop of adding in the element.但是,因为它是自上而下进行的,所以它会进入添加元素的连续循环。

I tried using the .previousElementSibling but that was returning null for a lot of the elements in the list when trying to insert it to the element above.我尝试使用.previousElementSibling但在尝试将其插入到上面的元素时,它为列表中的许多元素返回null

This is where I'm up to, and hopefully someone can guide me to the correct way of working this data.这就是我要做的,希望有人能指导我正确处理这些数据的方法。

 // -- get the sidebar list const sidebarList = document.querySelector('div.sidebar-nav > ul'); const reorder = ((list) => { const listItems = list.querySelectorAll(':scope li'); listItems.forEach((element) => { // -- last h6 wont have a next sibling if (element.nextElementSibling === null) return; // -- if it is a second sub heading skip if (element.nextElementSibling.tagName;== 'UL') return. // -- add into the next element element.innerHTML = element.innerHTML + element.nextElementSibling;innerHTML }); })(sidebarList);
 /* ------ HOW IT SHOULD RENDER ------ <aside class="sidebar"> <div class="sidebar-nav"> <ul> <li> <a>Heading level 1</a> <ul> <li> <a>Heading level 2</a> <ul> <li> <a>Heading level 3</a> <ul> <li> <a>Heading Level 4 - 1</a> </li> <li> <a>Heading Level 4 - 2</a> <ul> <li> <a>Heading Level 5</a> <ul> <li> <a>Heading level 6</a> </li> </ul> </li> </ul> </li> </ul> </li> </ul> </li> </ul> </li> <li> <a>Heading level 1</a> <ul> <li> <a>Heading level 2</a> <ul> <li> <a>Heading level 3</a> <ul> <li> <a>Heading Level 4 - 1</a> </li> <li> <a>Heading Level 4 - 2</a> <ul> <li> <a>Heading Level 5</a> <ul> <li> <a>Heading level 6</a> </li> </ul> </li> </ul> </li> </ul> </li> </ul> </li> </ul> </li> </ul> </div> </aside> */
 <aside class="sidebar"> <div class="sidebar-nav"> <ul> <li><a>Heading level 1</a></li> <ul> <li><a>Heading level 2</a></li> <ul> <li><a>Heading level 3</a></li> <ul> <li><a>Heading Level 4 - 1</a></li> <li><a>Heading Level 4 - 2</a></li> <ul> <li><a>Heading Level 5</a></li> <ul> <li><a>Heading level 6</a></li> </ul> </ul> </ul> </ul> </ul> <li><a>Heading level 1</a></li> <ul> <li><a>Heading level 2</a></li> <ul> <li><a>Heading level 3</a></li> <ul> <li><a>Heading Level 4 - 1</a></li> <li><a>Heading Level 4 - 2</a></li> <ul> <li><a>Heading Level 5</a></li> <ul> <li><a>Heading level 6</a></li> </ul> </ul> </ul> </ul> </ul> </ul> </div> </aside>

This seems to work:这似乎有效:

document.querySelectorAll('ul').forEach(function(list) {
  let prev = list.previousElementSibling;

  if (!prev) return;

  if (list.parentNode.nodeName == "UL" && prev.nodeName == "LI") {
    prev.appendChild(list);
  }
});

It goes through every ul , checking whether the ul has a direct ul parent and preceding li .它遍历每个ul ,检查ul是否有直接的ul父级和前面的li If it does, it appends the ul to the preceding li .如果是这样,它将ul附加到前面的li

https://jsfiddle.net/ago8by90/ https://jsfiddle.net/ago8by90/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM