javascript实现动态生成(页面元素)以表格为例

来源:互联网 发布:杜蕾斯淘宝快递 编辑:程序博客网 时间:2024/06/05 20:48
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title> New Document </title>  <meta name="Generator" content="EditPlus">  <meta name="Author" content="">  <meta name="Keywords" content="">  <meta name="Description" content=""> </head> <body>   <script type="text/javascript"> var table = document.createElement("table"); table.setAttribute("border","1px"); table.setAttribute("width","500px"); table.setAttribute("borderColor","red"); table.setAttribute("height","500px"); table.setAttribute("cellSpacing","0"); var tbody = document.createElement("tbody"); document.body.appendChild(table); table.appendChild(tbody); //定义颜色数组 var arr = new Array("#0000FF","#808080","#800080") for(i=0;i<3;i++){var tr = document.createElement("tr");tbody.appendChild(tr);for(j=0;j<3;j++){var td = document.createElement("td");td.setAttribute("bgColor",arr[i]);var text = document.createTextNode("aa");td.appendChild(text);tr.appendChild(td);} }   </script> </body></html>