我无法在列表元素中对元素进行分组。
例如,我有三个列表元素:
<ul class="row">
<li class="item">
<!-- product name -->
<div class="imglist">
<a href="#" title="image"><span class="txt">txt</span></a>
</div>
</li>
</ul>
<ul class="row">
<li class="item">
<!-- product name -->
<div class="imglist">
<a href="#" title="image"><span class="txt">txt</span></a>
<a href="#" title="image"><span class="txt">txt</span></a>
<a href="#" title="image"><span class="txt">txt</span></a>
<a href="#" title="image"><span class="txt">txt</span></a>
<a href="#" title="image"><span class="txt">txt</span></a>
<a href="#" title="image"><span class="txt">txt</span></a>
</div>
</li>
</ul>
<ul class="row">
<li class="item">
<!-- product name -->
<div class="imglist">
<a href="#" title="image"><span class="txt">txt</span></a>
<a href="#" title="image"><span class="txt">txt</span></a>
</div>
</li>
</ul>
在 Jquery 我试过这个:
var divs = $("div.imglist > a");
for(var i = 0; i < divs.length; i+=4) {
divs.slice(i, i+4).wrapAll("<div class='group'></div>");
}
但现在的结果是:
<ul class="row">
<li class="item">
<!-- product name -->
<div class="imglist">
<div class="group">
<a href="#" title="image"><span class="txt">txt</span></a>
<a href="#" title="image"><span class="txt">txt</span></a>
<a href="#" title="image"><span class="txt">txt</span></a>
<a href="#" title="image"><span class="txt">txt</span></a></div>
</div>
</li>
<li class="item">
<!-- product name -->
<div class="imglist">
<div class="group">
<a href="#" title="image"><span class="txt">txt</span></a>
<a href="#" title="image"><span class="txt">txt</span></a>
<a href="#" title="image"><span class="txt">txt</span></a>
<a href="#" title="image"><span class="txt">txt</span></a></div>
</div>
</li>
<li class="item">
<!-- product name -->
<div class="imglist">
<div class="group">
<a href="#" title="image"><span class="txt">txt</span></a></div>
</div>
</li>
</ul>
我的问题是我想要每个li.item
单独的包装。