我無法對列表元素中的元素進行分組。jquery單獨列表元素的每四個子元素包裝
對於excample我有三個列表元素:
<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
。
非常感謝 – eyeonu 2013-02-12 12:38:25