注:1、此實例是驗證二維數組兩種賦值方式:1)先聲明后賦值。2)聲明的同時賦值。
2、將數組元素輸出到表格中。(表格的一個關鍵屬性:border-collapse:collapse;合並表格單元格邊框。)
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>無標題文檔</title>
<script type="text/javascript">
function getValue(){
/*二維數組的聲明*/
var myarray1 = new Array(2);//聲明2行
myarray1[0] = new Array(3);//第一行有3列;以下同理。
myarray1[1] = new Array(3);
/*該數組存儲的是表格單元格的地址,用於顯示輸出的數組值*/
var location = new Array(2)
location[0] = [document.getElementById("table00"),document.getElementById( "table01"),document.getElementById("table02")];
location[1] = [document.getElementById("table10"),document.getElementById("table11"),document.getElementById("table12")];
/*二維數組第一種賦值方法*/
myarray1 = [[1,2,3],[4,5,6]] //外面括號不要使用{}
/*使數組myarrya1中的值輸出到表格中*/
for(var row=0;row < 2;row++){
for(var col=0;col < 3;col++){
var flag = location[row][col];
flag.innerHTML= myarray1[row][col];
}
}
/*二維數組第二種賦值方法*/
var location2 = new Array([document.getElementById("cell00"),document.getElementById("cell01"),document.getElementById("cell02")],[document.getElementById("cell10"),document.getElementById("cell11"),document.getElementById("cell12")])
/*直接調用內置函數prompt()輸入數組各個元素值,並且將數組元素值輸出到location2對應的表格地址中*/
for(var row=0;row < location2.length;row++){
for(var col=0;col < location2[row].length;col++){
location2[row][col].innerHTML = prompt("Enter a value:","anynumble or string");
}
}
}
</script>
<style type="text/css">
#mytable{border:2px inset #999;border-collapse:collapse;width:200px;height:100px;text-align:center;}
/*border-collapse:collapse表示合並表格單元格邊框*/
tr,td{border:1px solid #666;}
#mytable2{border:2px inset #999;border-collapse:collapse;width:200px;height:100px;text-align:center;}
/*
groove 定義 3D 凹槽邊框。其效果取決於 border-color 的值。
ridge 定義 3D 壟狀邊框。其效果取決於 border-color 的值。
inset 定義 3D inset 邊框。其效果取決於 border-color 的值。
outset 定義 3D outset 邊框。其效果取決於 border-color 的值。
*/
</style>
</head>
<body onLoad="getValue()">
<table id="mytable">
<caption>數組一元素表</caption>
<tr>
<td id="table00">00</td>
<td id="table01">01</td>
<td id="table02">02</td>
</tr>
<tr>
<td id="table10">10</td>
<td id="table11">11</td>
<td id="table12">12</td>
</tr>
</table>
<hr/>
<table id="mytable2">
<caption>數組二元素表</caption>
<tr>
<td id="cell00">00</td>
<td id="cell01">01</td>
<td id="cell02">02</td>
</tr>
<tr>
<td id="cell10">10</td>
<td id="cell11">11</td>
<td id="cell12">12</td>
</tr>
</table>
</body>
</html>
備注:由於是新人,若有不當之處,請諒解之處。謝謝!