我們可以利用document.createElement()動態的產生HTML的element,再利用Node.appendChild()插入某個Node底下,或利用Node.removeChild()自某個Node底下移除,更多資訊可以參考[1]。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>auto table</title> </head> <body> <table id="brook" summary=""></table> <script type="text/javascript"> //<![CDATA[ function create_new_row_button(eT, msg) { var eR = document.createElement("tr"); var eC = document.createElement("td"); var eB = document.createElement("input"); eB.type = "button" eB.value= msg; eB.style.width = "100px"; eT.appendChild(eR); eR.appendChild(eC); eC.appendChild(eB); } var station = [ {name: "CHT"}, {name: "TW"}, ]; for (var i = 0; i < station.length; i++) { create_new_row_button(document.getElementById("brook"), station[i].name); } //]]> </script> </body> </html>
沒有留言:
張貼留言