如何将div与clone合并

时间:2014-12-24 10:45:05

标签: javascript jquery html

HTML代码

<div id="parent">
    <div class="child">Content1...</div>
    <div class="child">Content2...</div>
    <div class="child">Content3...</div>
    <div class="child">Content4...</div>
    <div class="child">Content5...</div>
    <div class="child">Content6...</div>
</div>

jQuery代码

clone_object = $("#parent>div:lt(2)").clone();

我尝试了以下代码,但没有给出任何结果。

clone_object.add($("<div class='clear'></div>"));

预期输出

<div class="child">Content1...</div>
<div class="child">Content2...</div>
<div class='clear'></div>

我的问题是如何将div与创建的克隆合并?

2 个答案:

答案 0 :(得分:0)

无需使用jquery来清除浮动。使用css如下所示:

#parent:after{
    content: "";
    clear: both;
    display: block;
}

<强> Example Fiddle

答案 1 :(得分:0)

clone_object = $("#parent>div:lt(2)").clone();
newObj = $('<div class="clear"></div>');
var obj = $.extend({}, clone_object, newObj);

console.log(obj);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="parent">
    <div class="child">Content1...</div>
    <div class="child">Content2...</div>
    <div class="child">Content3...</div>
    <div class="child">Content4...</div>
    <div class="child">Content5...</div>
    <div class="child">Content6...</div>
</div>