本文為大家介紹下使用js是如何創建元素(節點)的,感興趣的朋友不要錯過
代碼如下: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>jquery2</title> <script type="text/javascript"> //創建節點 var addItems = function() { document.getElementById("div1").innerHTML = ""; var value = parseInt(document.getElementById('itemsNumber').value); for (var i = 0; i < value; i++) { var input = document.createElement("input"); input.setAttribute("type", "text"); var br = document.createElement("br"); document.getElementById("div1").appendChild(input); document.getElementById("div1").appendChild(br); } } </script> </head> <body> <input type="text" id="itemsNumber"> <input type="button" id="btn" value="click" onclick="addItems();" > <div id="div1"></div> </body> </html>