简体   繁体   English

如何用相同的 class 但不同的 position 包装所有元素

[英]How to wrapAll element with the same class but different position

I have a problem with Javascript, my HTML list has five items with .item class, I want it to wrapAll by ul element but, when I use the wrapAll method of jQuery, so it wraps all .item elements with one ul , I want it to be a different ul separated by other li elements like the code below: I have a problem with Javascript, my HTML list has five items with .item , I want it to wrapAll by ul element but, when I use the wrapAll method of jQuery, so it wraps all .item elements with one ul , I want它是一个不同的ul ,由其他li元素分隔,如下面的代码:

<ul>
   <li>Item 1</li>
   <li class='item'>Item 2</li>
   <li class='item'>Item 3</li>
   <li>Item 4</li>
   <li>Item 5</li>
   <li class='item'>Item 6</li>
   <li class='item'>Item 7</li>
   <li class='item'>Item 8</li>
   <li>Item 9</li>
   <li>Item 10</li>
</ul>

And I want to wrapAll the .item class like this:我想像这样包装所有.item wrapAll

<ul>
   <li>Item 1</li>
   <ul class='wrap'>
      <li class='item'>Item 2</li>
      <li class='item'>Item 3</li>
   </ul>
   <li>Item 4</li>
   <li>Item 5</li>
   <ul class='wrap'>
      <li class='item'>Item 6</li>
      <li class='item'>Item 7</li>
      <li class='item'>Item 8</li>
   </ul>
   <li>Item 9</li>
   <li>Item 10</li>
</ul>

I've tried this:我试过这个:

$('.item').wrapAll($('<ul/>',{class:'wrap'}));

but it goes like this:但它是这样的:

<ul>
   <li>Item 1</li>
   <ul class='wrap'>
      <li class='item'>Item 2</li>
      <li class='item'>Item 3</li>
      <li class='item'>Item 6</li>
      <li class='item'>Item 7</li>
      <li class='item'>Item 8</li>
   </ul>
   <li>Item 4</li>
   <li>Item 5</li>
   <li>Item 9</li>
   <li>Item 10</li>
</ul>

Thanks for reading.谢谢阅读。

One idea could be to select all li elements which do not have an item class which have an adjacent li item with an .item class.一个想法可能是 select 所有不具有item class 的li元素,这些元素具有具有 .item .item的相邻li项目。 This essentially will give you all the li elements at the start of a group of items.这基本上会给你一组项目开头的所有li元素。 You can then use .nextUntil(':not(.item)') on each starting li to get each li element after your first .item which has the .item class.然后,您可以在每个起始 li 上使用.nextUntil(':not(.item)')来获取第一个具有.item .item的 .item 之后的每个li元素。 This gives you all elements but your original starting element, so you need to use .addBack() to add the initial starting element to the collection.这将为您提供除原始起始元素之外的所有元素,因此您需要使用.addBack()将初始起始元素添加到集合中。 You can then wrap this using wrapAll() :然后,您可以使用wrapAll()包装它:

 $('li:not(.item) + li.item').each(function() { $(this).nextUntil(':not(.item)').addBack().wrapAll($('<ul/>',{class:'wrap'})); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul> <li>Item 1</li> <li class='item'>Item 2</li> <li class='item'>Item 3</li> <li>Item 4</li> <li>Item 5</li> <li class='item'>Item 6</li> <li class='item'>Item 7</li> <li class='item'>Item 8</li> <li>Item 9</li> <li>Item 10</li> </ul>

Here, I made a pure JavaScript solution:在这里,我做了一个纯JavaScript的解决方案:

 var item = document.querySelectorAll("ul")[0].children; var arr = []; for (var i=0; i<item.length; i++) { if (item[i].className == "item") { arr.push(item[i]); var nextEle = item[i].nextElementSibling; if (nextEle.className.== "item") { var newGroup = document;createElement("UL"). newGroup,setAttribute("class"; "wrap"); for (var x=0. x<arr;length. x++) { console;log(arr[x]). newGroup;appendChild(arr[x]). } nextEle.parentNode,insertBefore(newGroup; nextEle); arr = []; } } }
 <ul> <li>Item 1</li> <li class='item'>Item 2</li> <li class='item'>Item 3</li> <li>Item 4</li> <li>Item 5</li> <li class='item'>Item 6</li> <li class='item'>Item 7</li> <li class='item'>Item 8</li> <li>Item 9</li> <li>Item 10</li> </ul>

This took me a while, but does exactly what you need.这花了我一段时间,但正是你需要的。

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

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