简体   繁体   English

如何在父div下包装连续的兄弟div?

[英]How can I wrap contiguous sibling divs under a parent div?

I have some HTML that looks like this: 我有一些看起来像这样的HTML:

<p>Some Text</p>
<div class="listBullet">Item 1</div>
<div class="listBullet">Item 2</div>
<div class="listBullet">Item 3</div>
<p>Some More Text</p>
<div class="listBullet">Item 1</div>
<div class="listBullet">Item 2</div>
<div class="listBullet">Item 3</div>
<p>Some Other Text</p>

I want to end up with the following output: 我想最终得到以下输出:

<p>Some Text</p>
<div class="wrapperDiv">
   <div class="listBullet">Item 1</div>
   <div class="listBullet">Item 2</div>
   <div class="listBullet">Item 3</div>
</div>
<p>Some More Text</p>
<div class="wrapperDiv">
   <div class="listBullet">Item 1</div>
   <div class="listBullet">Item 2</div>
   <div class="listBullet">Item 3</div>
</div>
<p>Some Other Text</p>

I tried $(".listBullet").wrapAll("<div class='wrapperDiv' />") , but that ended up moving the two blocks to be contiguous with each other. 我尝试了$(".listBullet").wrapAll("<div class='wrapperDiv' />") ,但最终$(".listBullet").wrapAll("<div class='wrapperDiv' />")两个块移动到彼此连续。 It seems like what I need is a selector that separates the contiguous blocks into separate elements, which I would then call wrapAll on separately. 看起来我需要的是一个选择器,它将连续的块分隔成单独的元素,然后我将单独调用wrapAll。

This does the job: 这样做的工作:

$('p + .listBullet').each(function() {
  $(this).nextUntil('p')
         .addBack()
         .wrapAll("<div class='wrapperDiv' />");
});

Fiddle 1 小提琴1


If your XHTML has a mix of elements, you can do this (assuming container is the class of the parent div ): 如果你的XHTML有各种元素,你可以这样做(假设container是父div的类):

$('.container > :not(.listBullet) + .listBullet').each(function() {
  $(this).nextUntil('.container > :not(.listBullet)')
         .addBack()
         .wrapAll("<div class='wrapperDiv' />");
});

Fiddle 2 小提琴2


Here's a more brute force approach: 这是一种更强力的方法:

 var lb= []; $('.container > *').each(function() { if($(this).hasClass('listBullet')) { lb.push(this); } else { $(lb).wrapAll("<div class='wrapperDiv'/>"); lb= []; } }); $(lb).wrapAll("<div class='wrapperDiv'/>"); 

Fiddle 3 小提琴3

As a general approach you could: 作为一般方法,您可以:

Loop through all the elements on the page using .next() , while the next element you find is has the correct class, (use .attr("class") ) add an extra class of currentList (or simular) class wrapAll on currentList then select all the items with the currentList class and remove that class and then keep looping! 使用通过网页上的所有元素循环.next()当你找到下一个元素是具有正确的类(使用.attr("class")添加一个额外的类的currentList (或simular)类wrapAll currentList然后用currentList类选择所有项目并删除该类,然后继续循环!

Well, you could also do something like this , although maybe just using jQuery methods is more straightforward (and a bit more flexible): 好吧, 你也可以这样做 ,虽然可能只是使用jQuery方法更直接(并且更灵活):

http://jsfiddle.net/ewj44a2L/1/ http://jsfiddle.net/ewj44a2L/1/

 var listBullets = $('.listBullet'), n = 3, // Number of bullets per group. All groups must be equal. len = listBullets.length / n; // Number of groups to be encapsulated for(var i = 0; i < len; i++) { listBullets.slice( n * i, n * (i + 1) ) .wrapAll('<div class="wrapperDiv"></div>'); } 
 .wrapperDiv { background: #000; color: #fff; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p>Some Text</p> <div class="listBullet">Item 1</div> <div class="listBullet">Item 2</div> <div class="listBullet">Item 3</div> <p>Some More Text</p> <div class="listBullet">Item 1</div> <div class="listBullet">Item 2</div> <div class="listBullet">Item 3</div> <p>Some Other Text</p> <div class="listBullet">Item 1</div> <div class="listBullet">Item 2</div> <div class="listBullet">Item 3</div> 

Try 尝试

 $("p").map(function(i, el) { var el = $(el), list = ".listBullet"; if (el.next().is(list)) { var wrap = el.after("<div class=wrapperDiv />").next(); do { wrap.next().appendTo(wrap); } while (wrap.next().is(list)); }; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script> <p>Some Text</p> <div class="listBullet">Item 1</div> <div class="listBullet">Item 2</div> <div class="listBullet">Item 3</div> <p>Some More Text</p> <div class="listBullet">Item 1</div> <div class="listBullet">Item 2</div> <div class="listBullet">Item 3</div> <p>Some Other Text</p> 

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

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