JS控制網頁動態產生任意行列數表格的方法

來源:互聯網
上載者:User

JS控制網頁動態產生任意行列數表格的方法

 這篇文章主要介紹了JS控制網頁動態產生任意行列數表格的方法,執行個體分析了javascript動作表格節點控制dom元素添加的技巧,具有一定參考借鑒價值,需要的朋友可以參考下

 

 

本文執行個體講述了JS控制網頁動態產生任意行列數表格的方法。分享給大家供大家參考。具體分析如下:

這是一個非常簡單使用的JS線上產生表格的代碼效果
通過JS功能代碼,直接輸入行數和列數就可以自動產生你需要的表格
當然你也可以擴充JS代碼實現產生文字的各種形式

 

代碼如下:

<html>
<head>
<title>Js動態產生表格</title>
<style type="text/css">
table{font-size:14px;}
</style>
</head>
<body >
<script language="javascript">
function tableclick(name1,name2,name3){
Trow=name1.value;
Tcol=name2.value;
Tv=name3.value;
if ((Trow=="") || (Tcol=="") || (Tv=="")){
alert("請將製作表格的條件填寫完整");
}
else{
r=parseInt(Trow);
c=parseInt(Tcol);
Table1(r,c,Tv);
}
}
function tablevalue(a,ai,rows,col,str){
int1=a.length;
for (i=0;i<rows;++i){
for (j=0;j<col;++j){
if ((j==0)&&(ai>=int1)){break;}
if (ai>=int1){
str=str+"<td scope='col'> </td>";
}
else{
if (j==0){
str=str+"<tr><th scope='col'> "+(a[ai++])+"</th>";
}
else{
if (j==col-1){
str=str+"<td scope='col'> "+(a[ai++])+"</td>";
}
else{
str=str+"<td scope='col'> "+(a[ai++])+"</td>";
}
}
}
}
str=str+"</tr>";
}
return str;
}
function Table1(row,col,Str1){
var str="";
a=new Array();
s=new String(Str1);
a=s.split("#");
int1=a.length;
ai=0;
if (col<=int1){
str=str+"<table width='300' border='4'>";
for (i=0;i<col;++i){
if (i==0){
str=str+"<tr><th scope='col'> "+(a[ai++])+"</th>";
}
else{
if (i==(col-1)){
str=str+"<th scope='col'> "+(a[ai++])+"</th></tr>";
}
else{
str=str+"<th scope='col'> "+(a[ai++])+"</th>";
}
}
}
if (int1>col){
if (row>1){
str=tablevalue(a,ai,row-1,col,str);
}
}
str=str+ "</table>";
aa.innerHTML=str;
}
}
</script>
<form name="form1" method="post" action="">
<p><b>行數:</b>
<input name="name1" type="text" style="width:40px" value="4">
<b>列數:</b>
<input name="name2" type="text" style="width:40px" value="4">
<input type="button" name="Submit3" value="產生表格" onClick="tableclick(document.form1.name1,document.form1.name2,document.form1.name3)"></p>
<p><b align="top">表值:</b></p>
<p>
<input name="name3" wrap="VIRTUAL" style="width:520px " value="COL1#COL2#COL3#COL4#ROW1#A1#A2#a3#ROW2#B1#B2#B3#ROW3#C1#C2#C3">
</p>
</form>
<div id="aa"></div>
</body>
</html>

 

希望本文所述對大家的javascript程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.