2017-10-09 17 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