最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - wrapAll jQuery problem - Stack Overflow

programmeradmin2浏览0评论

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" />

What I'm trying to achieve is this:

<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>

This is wrapping everything into one div:

$('.one, .two, .three').wrapAll('<div class="wrap" />')

How can I get the groups wrapped separately?

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" />

What I'm trying to achieve is this:

<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>

This is wrapping everything into one div:

$('.one, .two, .three').wrapAll('<div class="wrap" />')

How can I get the groups wrapped separately?

Share Improve this question edited Oct 6, 2009 at 21:01 sth 230k56 gold badges287 silver badges370 bronze badges asked Oct 6, 2009 at 20:29 DomDom 3,12614 gold badges48 silver badges69 bronze badges 1
  • so each set of div.one div.two div.three needs to be wrapped in div.wrap? – Colin Commented Oct 6, 2009 at 20:41
Add a ment  | 

5 Answers 5

Reset to default 5
$(function(){
    var all = $('.one, .two, .three');
    for(i=0; i<all.length; i+=3){
        all.slice(i,i+3).wrapAll('<div class="wrap" />');
    }
});

See it in action: http://jsbin./uqosa/

By the way, <div class="one" /> didn't work for me on Firefox, I had to close them with </div>.

Try this:

$('div.one').each(function() {
    var wrap = $(this).wrap('<div class="wrap"></div>').parent();
    wrap.next('div.two').appendTo(wrap);
    wrap.next('div.three').appendTo(wrap);
});

This will work:

$(function() {
    var $ones = $('div.one');
    var $twos = $('div.two');
    var $threes = $('div.three');

    $ones.each(function(idx) {
        $('<div class="wrap"></div>')
            .append($ones.eq(idx))
            .append($twos.eq(idx))
            .append($threes.eq(idx))
            .appendTo('body');
    });
});

In my head this should work:

$('.one').each(function() {
    var wrap = $('<div>').addClass('wrap'), two = $(this).next(), three = $(two).next();
    $(this).after(wrap).appendTo(wrap)
    two.appendTo(wrap), three.appendTo(wrap)
});
$.fn.matchUntil = function(expr) {
    var match = [];
    this.each(function(){
        match = [this];
        for ( var i = this.nextSibling; i; i = i.nextSibling ) {
            if ( i.nodeType != 1 ) { continue; }
            if ( $(i).filter(expr).length > 0 ) { break; }
            match.push( i );
        }
    });
    return this.pushStack( match );
};

usage:

$(".one").each( function() {
    $(this).matchUntil(".one").wrapAll("<div class='wr'></div>");
})

it's just modified version of http://docs.jquery./JQuery_1.2_Roadmap#.nextUntil.28.29_.2F_.prevUntil.28.29, which doesn't seem to work with new jQuery.

发布评论

评论列表(0)

  1. 暂无评论