简体   繁体   English

如何将每个两个div附加到div?

[英]How to append each two divs to a div?

I have this list: 我有这个清单:

<div id="list">
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
</div>

I want to move each two elements (.item) inside a div with class="parent" . 我想在每个带有class="parent"的div中移动两个元素(.item)。 I tried with the following, but it doesn't work: 我尝试了以下操作,但不起作用:

var parent = $("<div class='parent'></div>");
$("#list .item:nth-child(1), #list .item:nth-child(2)").appendTo(parent);
$("#list .item:nth-child(3), #list .item:nth-child(4)").appendTo(parent);
$("#list .item:nth-child(5), #list .item:nth-child(6)").appendTo(parent);

jsFiddle 的jsfiddle

parent is a single cached element so each time you use it it will not create a new parent. parent是单个缓存的元素,因此每次使用它都不会创建新的父元素。 Not sure if you looking for something like .wrap()? 不知道您是否正在寻找类似.wrap()的东西? http://api.jquery.com/wrap/ or .wrappAll() http://api.jquery.com/wrapAll/ http://api.jquery.com/wrap/或.wrappAll() http://api.jquery.com/wrapAll/

$('#list').children().wrapAll('<div class="parent"/>');

Store the childrens in an array, then create as many parents as needed and put them back in your list, here is the code: 将孩子存储在一个数组中,然后根据需要创建尽可能多的父母,并将其放回列表中,代码如下:

var list = $("#list"),
    items = $.makeArray($(".item"));

list.html("");
for (var i=0; i < items.length; i++) {
    if (i%2 == 0) list.appendChild($('<div class="parent"/>'));
    $("#list div:last-child").appendChild(items[i]);
}

You can wrap each two child element like this: 您可以像这样包装每两个子元素:

var childLists= $('.item');
for(var i = 0, l = childLists.length; i < l; i += 2) {
    childLists.slice(i, i+2).wrapAll('<div class="parent"></div>');
}

You can use wrapAll() method as follows: 您可以如下使用wrapAll()方法:

 $(".item:even").each(function(){ $(this).next().addBack().wrapAll("<div class='parent'></div>"); }) 
 *{ margin:0; padding:0; } body{ background:silver; } .parent{ height:100px; padding:5px; margin:5px; text-align:center; background:dodgerblue; } .item{ height:40px; line-height:40px; margin:5px 0; background:#fff; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="list"> <div class="item">1</div> <div class="item">2</div> <div class="item">3</div> <div class="item">4</div> <div class="item">5</div> <div class="item">6</div> </div> 

you can try this:without wrap you can get it. 您可以尝试以下方法:无需包装即可获得。

<script>
$(document).ready(function(){
var parent = document.createElement("div"); 
parent.className = "parent";
x = document.getElementById("list");
parent.appendChild(x);
document.body.appendChild(parent);
});
</script>

<div id="list">
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM