JavaScript向OL列表内动态添加LI元素的方法,下面JS代码每次点击按钮都会想OL列表中动态添加一个LI
<script type="text/javascript"> function addItem() { var myitem = document.getElementById("ItemToAdd").value; var mylistItems = document.getElementById("mylist"); var newP = document.createElement("li"); var textNode = document.createTextNode(myitem); newP.appendChild(textNode); document.getElementById("mylist").appendChild(newP); return false; } </script> <form onsubmit="return addItem()" action="#"> <span>Grocery Items:</span> <input type="text" id="ItemToAdd" value="Milk" /> <input type="button" value="Add" onclick="addItem()" /> </form> <span>Grocery List:</span> <ol id="mylist"></ol>