javascript 动态创建表格的2种方法总结
第一种方法:
<html>
<head>
<script>
functioncreateTable(rows,lines){
this.rows=rows;
this.lines=lines;
varBody=document.getElementById('body');
varTable=document.createElement('table');//创建table标签元素
Table.setAttribute('border','1');
//给table标签添加其他属性
for(vari=0;i<this.rows;i++){
varlRow=document.createElement('tr');
for(varj=0;j<this.lines;j++){
vartextNode=document.createTextNode(i+','+j);
varlLine=document.createElement('td');
lLine.appendChild(textNode);
lRow.appendChild(lLine);
}
Table.appendChild(lRow);
}
Body.appendChild(Table);
}
</script>
</head>
<body>
<divid="body"></div>
</body>
<scripttype="text/javascript">
createTable(10,10);
</script>
</html>
第二种方法:
<script>
functioncreateTable(rows,lines){
this.rows=rows;
this.lines=lines;
varBody=document.getElementById('body');
varTable=document.createElement('table');
Table.setAttribute('border',1);
for(vari=0;i<this.rows;i++){
varrow=Table.insertRow(i);
for(varj=0;j<this.lines;j++){
varcells=row.insertCell(j);
cells.innerHTML=i+','+j
}
}
Body.appendChild(Table);
}
</script>
以上2种方法小伙伴们是否都了解了呢,如有更好的方法,也请大家留言说明下,大家共同进步。
热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短