0

我有一个day班级动态:

<div class="day day111">a</div>
<div class="day day111">a</div>
<div class="day day112">a</div>
<div class="day day112">a</div>
<div class="day day113">a</div>

我试图得到这个:

<div class="group">
    <div class="day day111">a</div>
     <div class="day day111">a</div>
</div>
<div class="group">
    <div class="day day112">a</div>
    <div class="day day112">a</div>
</div>
<div class="group">
    <div class="day day113">a</div>
</div>

我只能在javascript中做到这一点。我正在尝试循环,但我无法分组。你能帮助我吗?谢谢!

4

2 回答 2

3

尝试

var groups = {};
$('.day').each(function () {
    var className = this.className.match(/(day\d+)/)[1],
        $group = groups[className];
    if (!$group) {
        $group = $('<div />', {
            'class': 'group'
        }).insertAfter(this);
        groups[className] = $group;
    }
    $group.append(this);
})

演示:小提琴

于 2013-11-13T09:09:33.393 回答
0

看看这个:

$('#box1 >div.day').each(function(){
   var cls = (this.className);

    var group = groups[cls];
    if(!group)
    {
    group = $('<div/>').appendTo('#box2');
    group.addClass('group');

    groups[cls] = group;
    }

    $(this).appendTo(group);
});

http://jsfiddle.net/p3YTS/2/

于 2013-11-13T09:14:07.393 回答