简体   繁体   English

如何使用jQuery追加包含多个元素的元素(div)?

[英]How to append an element (div) that contains multiples elements inside using jQuery?

I have an object <div> that contains many divs inside. 我有一个对象<div> ,其中包含许多div。 And a simple button. 还有一个简单的按钮。 What I want is to create the first object any time the button is clicked. 我想要的是在点击按钮的任何时候创建第一个对象。

For now I can only create the object but empty, how can I do to create the div with all their child (the "hello" inside)? 现在我只能创建对象但是空了,我怎么能用他们所有的孩子创建div(里面的“你好”)?

Here is a simple script 这是一个简单的脚本

 $(".button").click(function(){ var div = document.createElement('div'); div.className = 'object'; $(".container").append(div); }); 
 .object{ margin-top:10px; width:100px; height:50px; background-color:red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <button class="button" type="button"> create an object </button> <div class="container"> <div class="object"> <p> hello </p> </div> </div> 

Here is one possible solution. 这是一种可能的解决方案。 Your container div can be thought of as a container of child div elements. 您的容器div可以被认为是子div元素的容器。 So you will want to clone your object, and then append it to this container. 因此,您需要克隆对象,然后将其附加到此容器中。

Check out this snippet. 看看这个片段。

 $(".button").click(function(){ $(".object:first").clone().appendTo("div.container"); }); 
 .object{ margin-top:10px; width:100px; height:50px; background-color:red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <button class="button" type="button"> create an object </button> <div class="container"> <div class="object"> <p> hello </p> </div> </div> 

Another possible solution is to use html() 另一种可能的解决方案是使用html()

 $(".button").on("click", function() { $(".container").html($(".container").html() + "<div class='object'><p>Hello</p></div>"); }); 
 .object{ margin-top:10px; width:100px; height:50px; background-color:red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <button class="button" type="button"> create an object </button> <div class="container"> <div class="object"> <p> hello </p> </div> </div> 

If you want it to be a copy of the first item you can use clone() . 如果您希望它是第一个项目的副本,则可以使用clone() If the element is not supposed to be in there to start, than add the element somewhere else to the page and select it. 如果元素不应该在那里开始,那么将元素添加到页面的其他地方并选择它。

 $(".button").click(function(){ var cont = $(".container"), div = cont.find(".object").eq(0).clone(); cont.append(div); }); 
 .object{ margin-top:10px; width:100px; height:50px; background-color:red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <button class="button" type="button"> create an object </button> <div class="container"> <div class="object"> <p> hello </p> </div> </div> 

Here you go...clone it with true which means it copies everything including events etc... then append that clone. 在这里你去...用true克隆它,这意味着它复制包括事件等在内的所有内容......然后附加该克隆。

If i helped please vote for my answer. 如果我帮忙请投票给我答案。 :) :)

 $(".button").click(function(){ /*var div = document.createElement('div'); div.className = 'object'; $(".container").append(div);*/ //new stuff var clone = $(".object").clone(true); $(".container").append(clone); }); 
 .object{ margin-top:10px; width:100px; height:50px; background-color:red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <button class="button" type="button"> create an object </button> <div class="container"> <div class="object"> <p> hello </p> </div> </div> 

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

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