简体   繁体   English

如何将这组li元素包装在ul容器中?

[英]How do wrap these groups of li elements in ul containers?

Goal: 目标:

First wrap the elements before the first empty div in a set of ul containers and then do the same for the elements that are between empty li s. 首先将第一个空div之前的元素包装在一组ul容器中,然后对空li之间的元素执行相同的操作。

The empty element can be included in the container or removed. 空元素可以包含在容器中或删除。

Question: 题:

How do I wrap these sets of elements in a set of ul containers if I can't target the class? 如果无法定位该类,如何将这些元素集包装在一组ul容器中?

Problem: 问题:

1) The only variable I can use is the empty li because there will be a different amount of elements in each container. 1)我可以使用的唯一变量是空li因为每个容器中元素的数量将有所不同。

2) The html doesn't end with an empty li . 2)html不以空li结尾。

HTML: HTML:

<div class="body">
<fieldset>
<ol>

<li>
    <h6>Customer Details</h6>
</li>
<li>
    <h6>Customer Details</h6>
</li>

wrap elements above 在上方包装元素

<li>&nbsp;</li>

wrap elements below until the next empty li 在下面包装元素,直到下一个空li

<li>
    <h6>Customer Details</h6>
</li>
<li>
    <h6>Customer Details</h6>
</li>
<li>
    <h6>Customer Details</h6>
</li>
<li>&nbsp;</li> 

wrap elements below until the next empty li 在下面包装元素,直到下一个空li

<li>
    <h6>Customer Details</h6>
</li>
<li>
    <h6>Customer Details</h6>
</li>
<li>
    <h6>Customer Details</h6>
</li>
<li>
    <h6>Customer Details</h6>
</li>

</ol>
</fieldset>
</div>

This is completely untested as I am on my phone, but I'm happy to update if there's syntax errors. 就像我在手机上一样,这完全未经测试,但是如果语法错误,我很乐意进行更新。 It relies on you being able to select/identify the parent to the HTML you have provided (assuming you have failed to add the final empty divider referred to in a comment above). 它取决于您是否能够选择/标识所提供HTML的父级(假设您未能添加上述注释中提到的最终的空分隔符)。

// Get the known parent to this mess of li elements
var knownParent = $(...);
// Give the empty li elements an identifying class
knownParent.find("li").filter(function(){ return $(this).html().match(/&nbsp;/) !== null; }).addClass("_liDivider");
// Wrap everything between the dividers with a ul element
$.each(
  $("._liDivider:not(:last-child)"), function() {
      $(this)
        .nextUntil("._liDivider")
        .wrap("<ul></ul>");
    }
);
// Now deal with the orphaned li elements before the first divider
knownParent.find("li:first-of-type").nextUntil("._liDivider").wrap("<ul></ul>");

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

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