wrapAll jQuery问题 [英] wrapAll jQuery problem
本文介绍了wrapAll jQuery问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将三个div包装成一个,但是这组三个div在代码中重复几次。也许我的HTML会解释:
I need to wrap three divs into one, but that group of three divs is repeating itself several times in code. Maybe my HTML will explain:
<div class="one" />
<div class="two" />
<div class="three" />
<div class="one" />
<div class="two" />
<div class="three" />
我想要实现的目标是:
<div class="wrap">
<div class="one" />
<div class="two" />
<div class="three" />
</div>
<div class="wrap">
<div class="one" />
<div class="two" />
<div class="three" />
</div>
这是将所有内容包装成一个div:
This is wrapping everything into one div:
$('.one, .two, .three').wrapAll('<div class="wrap" />')
如何将这些组分开包装?
How can I get the groups wrapped separately?
推荐答案
$(function(){
var all = $('.one, .two, .three');
for(i=0; i<all.length; i+=3){
all.slice(i,i+3).wrapAll('<div class="wrap" />');
}
});
查看实际操作: http://jsbin.com/uqosa/
顺便说一下,< div class = one/>
在Firefox上不适合我,我不得不用< / div>
关闭它们。
By the way, <div class="one" />
didn't work for me on Firefox, I had to close them with </div>
.
这篇关于wrapAll jQuery问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文