我想动态地添加更多的li元素,就像第一个一样,即通过按按钮来实现。这是一个不能工作的例子,位于jsfiddle上。
document.onload = init;
function init(){
document.getElementById('add').onclick = add;
}
function add(){
var el = document.getElementById('list');
var node = document.createElement("li");
var link = document.createElement("link");
link.setAttribute('href', 'www.google.it');
link.setAttribute('name', 'link');
node.appendChild(link);
el.appendChild(node);
}
<ul id="list">
<li>
<a href="www.google.it">link</a>
</li>
</ul>
<button id="add">Add link</button>
window.onload
而不是document.onload
。我不确定document.onload
会在什么时候被调用... - Rayon