废话不多说,直接给大家贴js代码了,具体代码如下所示:
//js实现输入表格行数、列数自动生成表格源代码<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>自动创建表格</title><style type="text/css">table{width:300px;height:100px;border:#0C9 1px solid;border-collapse:collapse;}</style><script type="text/javascript" language="javascript">function autocreate(){//创建table表格var table=document.createElement("table");table.setAttribute("border","1");table.setAttribute("background","red");//获取行数值var line=document.getElementById("line").value;//获取列数值var list=document.getElementById("list").value;for(var i=0;i<=line;i++){//alert(line);//创建trvar tr=document.createElement("tr");for(var j=0;j<=list;j++){//alert(list);//创建tdvar td=document.createElement("td");tr.appendChild(td);}table.appendChild(tr);}document.getElementById("d1").appendChild(table);}</script></head><body><input type="text" id="line">行数<input type="text" id="list">列数<input type="button" value="添加表格" onclick="autocreate()"><div id="d1" style="height:400px; width:300px;"></div></body></html>
以上所述是小编给大家介绍的基于JavaScript代码实现自动生成表格的全部叙述,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!
新闻热点
疑难解答