WEB开发网
开发学院网页设计JavaScript javascript循环数组的方法 阅读

javascript循环数组的方法

 2010-09-14 13:07:17 来源:WEB开发网   
核心提示:javascript数组;记录一下!一个数组:<script>varcolors={"1":{"a":"#000000","b":"#fdf6ed","c":"#634322&q

javascript数组;记录一下! 

一个数组:

<script>
  varcolors={  
   "1":{
   "a":"#000000",
   "b":"#fdf6ed",
   "c":"#634322",
   "d":"#c8ab8f",
   "e":"#fdf6ed"
   },
  
   "2":{
   "a":"#ba8247",
   "b":"#ffffff",
   "c":"#634320",
   "d":"#c8ffff",
   "e":"#ffffff"
   }
  };
</script>

显示方法:

functionselvalue(v){
vararrays=colors[v];
for(descriptioninarrays){
 document.getElementById(description+"color").value=arrays[description];
}
}

效果如下:

<!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>

Tags:javascript 循环 数组

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接