当前位置: 首页 > web开发 > 脚本语言 > 正文

javascript循环数组的方法

时间:2008-03-22

javascript数组;记录一下!

一个数组:

01.<script>
02. var colors = {
03. "1" : {
04. "a" : "#000000",
05. "b" : "#fdf6ed",
06. "c" : "#634322",
07. "d" : "#c8ab8f",
08. "e" : "#fdf6ed"
09. },
10.
11. "2" : {
12. "a" : "#ba8247",
13. "b" : "#ffffff",
14. "c" : "#634320",
15. "d" : "#c8ffff",
16. "e" : "#ffffff"
17. }
18. };
19.</script>

显示方法:

01.function selvalue(v){
02. var arrays = colors[v];
03. for(description in arrays) {
04. document.getElementById(description+"color").value = arrays[description];
05. }
06.}

效果,运行如下代码:

<!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>javascript test</title>
</head>
<body>
<form action="" method="get" >
<select id="selheigh" name="selheigh" onchange="selvalue(this.value);">
<option > 选一下 </option>
<option value="1" > 001 </option>
<option value="2"> 002 </option>
</select>
<br />
a:<input name="acolor" id="acolor" type="text"> <br />
b:<input name="bcolor" id="bcolor" type="text"> <br />
c:<input name="ccolor" id="ccolor" type="text"> <br />
d:<input name="dcolor" id="dcolor" type="text"> <br />
e:<input name="ecolor" id="ecolor" type="text"> <br />
</form>
<script>
var colors = {
"1" : {
"a" : "#000000",
"b" : "#fdf6ed",
"c" : "#634322",
"d" : "#c8ab8f",
"e" : "#fdf6ed"
},

"2" : {
"a" : "#ba8247",
"b" : "#ffffff",
"c" : "#634320",
"d" : "#c8ffff",
"e" : "#ffffff"
}
};

function selvalue(v){
var arrays = colors[v];
for(description in arrays) {
document.getElementById(description+"color").value = arrays[description];
}
}
</script>
</body>
</html>