var links = document.getElementsByTagName('a'); // links.length === 2 for instance. document.body.appendChild( links[0].cloneNode(true) ); // another link is added to the document // the 'links' NodeList is automatically updated // links.length === 3 now.
假设html代码如下
<div id="test">
<a href="#"></a>
</div>
JS
var links = document.getElementsByTagName("a"); //links.length === 2 var div = document.getElementById("test"); var a = document.createElement("a"); div.appendChild(a); //links.length===3
同样如果使用
div.appendChild(links[0]);
可以直接将第一个a标签放到最后,这是links中数组的顺序也改变