javascript循环数组的方法


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>

本文作者:
« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3