我可以克隆节点并附加到它的id数据吗?

时间:2017-04-24 06:14:33

标签: javascript jquery html

我有一些带有元素(类和ID)的id div,我需要克隆它并附加到我的克隆中以获取精确的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 :(得分: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);