jquery包装相邻元素的组

时间:2013-03-15 11:09:56

标签: jquery css dom

我有一个允许用户在页面上插入内容块的cms。用户可以使用不同类型的内容块,它们可以按任何顺序插入。高级dom结构的示例可能如下所示:

<p>Some rich text</p>

<div class="box">...</div>
<div class="box">...</div>
<div class="box">...</div>

<h3>Some  more rich text</h3>
<p>Lorem ipsum</p>

<div class="box">...</div>
<div class="box">...</div>

我想要做的是将任何相邻的'box'div包装在包装'容器'div中。因此,在上面的示例中,将插入两个“容器”div,因为有两组box div,导致:

<p>Some rich text</p>

<div class="container">
    <div class="box">...</div>
    <div class="box">...</div>
    <div class="box">...</div>
</div>

<h3>Some  more rich text</h3>
<p>Lorem ipsum</p>

<div class="container">
    <div class="box">...</div>
    <div class="box">...</div>
</div>

我认为有一种聪明的方法可以用css选择器来完成它,所以有人知道用jQuery做到这一点吗?

3 个答案:

答案 0 :(得分:4)

嗯,你可以这样做JSFiddle example我刚刚掀起。

这基本上循环遍历每个.box,将其添加到数组并确定下一个元素是否也具有.box类:

var collection = [];
$('.box').each(function() {
    var nextBox = $(this).next().hasClass('box');
    ...
    collection.push($(this));
})

如果下一个元素没有.box类,它会创建包含分隔符,将其放在{.box内的collection之前的页面上找到1}}数组,然后使用appendTo将所有.box分隔符移入其中:

    if(!nextBox)
    {
        var container = $('<div class="collection"></div>');
        container.insertBefore(collection[0]);
        for(i=0;i<collection.length;i++)
        {
            collection[i].appendTo(container);
        }
        collection = [];
    }

答案 1 :(得分:2)

小提琴在这里:http://jsfiddle.net/jdelight/XutA6/5/ 这是一个使用css的可能解决方案,它允许您使用单个背景颜色和边框设置块的样式。 HTML将是这样的:

    <div class="block">this is block 1</div>
    <div class="block">this is block 2</div>
    <div class="block">this is block 3</div>
    <div class="block">this is block 4</div>
    <div class="block">this is block 5</div>

CSS将是:

            /* style all blocks with the required background colour and border */
        .block {
            background: #eee;
            color: #000;
            border: 1px solid red;
            border-bottom: 0;
            padding: 20px;
            width: 400px;
            border-radius: 20px;
            /* remove the rounded corners from he bottom left/right */
            border-bottom-left-radius:0;
            border-bottom-right-radius:0;
            position: relative;
        }
        /* style all adjacent blocks and remove the radius - so the top block has a radius and the others don't */
        .block + .block {
            border-radius: 0;
            border-top: 0;
        }

        /* create a cheeky block with content after which sits below all blocks */
        /* so it's hidden from all the blocks above it apart from the very bottom one (see bottom: -10px) */
        /* then style the rounded corners on that one */
        .block::after {
            content:'.';
            display: block;
            background: red;
            height: 10px;
            position: absolute;
            border-bottom: 1px solid red;
            border-left:  1px solid red;
            border-right:  1px solid red;
            bottom: -10px;
            width: 440px;
            background: #eee;
            left:-1px;
            border-bottom-left-radius:10px;
            border-bottom-right-radius:10px;
        }

答案 2 :(得分:2)

您可以使用

  1. .nextUntil,获取下一个.box
  2. .andSelf将当前元素添加到集合
  3. .wrapAll将每个集合包装到不同的.container
  4. &#13;
    &#13;
    $('.box').not('.box+.box').each(function(){
      $(this).nextUntil(':not(.box)').addBack().wrapAll('<div class="container" />');
    });
    &#13;
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <p>Some rich text</p>
    
    <div class="box">...</div>
    <div class="box">...</div>
    <div class="box">...</div>
    
    <h3>Some  more rich text</h3>
    <p>Lorem ipsum</p>
    
    <div class="box">...</div>
    <div class="box">...</div>
    &#13;
    &#13;
    &#13;

    http://jsbin.com/gonino/edit?html,js