2017-04-24 65 views
0

我有一些id元素(類和ID)div,我需要克隆它並追加到我的克隆確切的Id數據。我該怎麼做?我可以克隆節點並附加到它的ID數據?

window.onload = Func(); 
 

 
function Func() { 
 

 
var temp = document.getElementById("start"); 
 
var cl = temp.cloneNode(true); 
 
var div = document.createElement('div'); 
 
div.className = "class"; 
 
div.innerHTML = "MyText"; 
 
var result = cl.getElementById("second").append(div); 
 
alert(result.innerHTML); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div id="start"> 
 
<div id="second"> 
 
<a href="#">Link</a> 
 
</div></div>

+1

哪裏是你的代碼的企圖? –

+0

顯示你有一些代碼,所以我們可以幫你... –

+1

https://www.google.co.in/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=clone+in+javascript檢查你是否從這裏得到了一些東西。 – Jai

回答

0
var item = document.getElementById("id1").lastChild; 
var clone = item.cloneNode(true); 
/*Id of the element below which you want to clone*/ 
document.getElementById("id2").appendChild(clone); 
+2

https://stackoverflow.com/help/how-to-answer – Jai