日期:2014-05-16 浏览次数:20384 次
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
#box {background-color: #667B99; height: 100px; width: 100px;}
</style>
<script>
var changeStyle = function (elem, attr, value)
{
elem.style[attr] = value
};
window.onload = function ()
{
var oBtn = document.getElementsByTagName("input");
var oDiv = document.getElementById("box");
var oAtt = ["width","height","background","display","display"];
var oVal = ["200px","200px","#2B91D5","none","block"];
var i = 0;
for(i=0; i<oBtn.length; i++){
oBtn[i].index = i;
oBtn[i].onclick = function(){
changeStyle(oDiv,oAtt[this.index],oVal[this.index]);
//请解释oAtt[this.index]和oVal[this.index]中的[this.index]为何不能直接用[i]。
}
}
};
</script>
</head>
<body>
<section>
<input type="button" value="变宽">
<input type="button" value="变高">
<input type="button" value="变色">
<input type="button" value="隐藏">
<input type="button" value="显示">
<div id="box"> </div>
</section>
</body>
</html>