我有以下HTML
<body>
<div class="myCool">blah</div>
<div class="cool">some text</div>
<div class="cool">some text</div>
<div class="cool">some text</div>
<div class="cool">some text</div>
<div class="myCool">blah</div>
<div class="cool">some text</div>
<div class="cool">some text</div>
<div class="cool">some text</div>
<div class="cool">some text</div>
</body>
我要做的是从myCool
开始,然后将其包含在接下来的4个班cool
中div
?所以它看起来像
<div id="1" class="myWrapped">
<div class="myCool">blah</div>
<div class="cool">some text</div>
<div class="cool">some text</div>
<div class="cool">some text</div>
<div class="cool">some text</div>
</div>
<div id="2" class="myWrapped">
<div class="myCool">blah</div>
<div class="cool">some text</div>
<div class="cool">some text</div>
<div class="cool">some text</div>
<div class="cool">some text</div>
</div>
我有什么想法可以做到这一点吗?
答案 0 :(得分:4)
试
$(".myCool").each(function(i){
$(this).nextUntil(".myCool").andSelf().wrapAll("<div id='" + (i+1) + "' class='myWrapped'></div>");
});
答案 1 :(得分:-2)
你可以这样做
jQuery("<div class='myCool'>blah</div>").appendTo('body');
for(var i=0; i<4;i++)
{
jQuery('<div class="cool">some text</div>').appendTo('.myCool');
}