下面我讲讲如何运用,先给出个没有结合ASP,就单纯适用javascript的例子。
1.js
var a = new Array();
var temp = "<table>";
a[0] = new Array(1,"阿会楠","男");
a[1] = new Array(2,"小白兔","女");
a[2] = new Array(3,"小琦","女");
for(var i = 0;i < a.length;i ++)
{
temp = temp + "<tr>"
for(var j = 0;j < a[0].length;j ++)
{
temp = temp + "<td>" + a[i][j] + "</td>";
}
temp = temp + "</tr>"
}
temp = temp + "</table>";
document.write(temp);
1.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312" />
<title>用数组存放数据</title>
</head>
<body>
<script language="JavaScript" type="text/javascript" src="1.js"></script>
</body>
</html>
1.html的输出结果:
1 阿会楠 男
2 小白兔 女
3 小琦 女
看到输出结果,你也许就想到我们该从那个js文件入手了,是的,我们把1.html中的 <script language="JavaScript" type="text/javascript" src="1.js"></script>
换成 <script language="JavaScript" type="text/javascript" src="1.asp"></script>
把1.js另存为1.asp并做如下修改:
var a = new Array();
var temp = "<table>";
<%
数据库连接代码,打开连接
For i = 1 To 3
If objRs.eof Then Exit For
%>
a[<%=i%>] = new Array(<%=objRs(0)%>,"<%=objRs(1)%>","<%=objRs(2)%>")
<%
objRs.movenext
Next
%>
for(var i = 0;i < a.length;i ++)
{
temp = temp + "<tr>"
for(var j = 0;j < a[0].length;j ++)
{
temp = temp + "<td>" + a[i][j] + "</td>";
}
temp = temp + "</tr>"
}
temp = temp + "</table>";
document.write(temp);
asp代码部分我并没有贴出完整的代码,只稍微写了一点,请自己补充。