廢話不多說,直接給大家貼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代碼實現自動生成表格的全部敘述,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!
新聞熱點
疑難解答