2017-10-09 81 views

回答

0
<html> 
<head> 
<script type="text/javascript"> 
function additem(){ 
var node = document.createElement("LI");    
var textnode = document.createTextNode("new li");  
node.appendChild(textnode);        
document.getElementById("myUl").appendChild(node); 
} 
</script> 
</head> 
<body> 
<ul id="myUl"></ul> 
<button onclick="additem()">Add</button> 
</body> 
</html> 

这是一个例子。

0

也许最简单的方式给功能键是这样的:

<button onMouseClick="addSomeElement();"> 

哪里addSomeElement();是你的appendChild功能如上所述。

0
<ul id="UL"> 

</ul> 
<button onclick="myFunction()">Try it</button> 

<script> 
function myFunction() { 
    var btn = document.createElement("BUTTON"); 
    btn.setAttribute('ID', 1); 
    btn.setAttribute('onclick','myfunction2()') 
    document.body.appendChild(btn); 

} 
function myfunction2(){ 
var ul = document.getElementById("UL"); 
var li = document.createElement("li"); 
li.appendChild(document.createTextNode("yourItem")); 

ul.appendChild(li); 
} 
</script> 

本应该做的,也是阅读的appendChild和看看这个例子有关创建按钮here和有关附加here