8.7w+
社区成员
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
.active{
background-color:#796ABF;
}
#div1 div{
width:200px;
height: 200px;
background: #CCC;
border:1px solid #000 ;
display: none;
}
</style>
<script type="text/javascript">
window.onload=function(){
var aDiv=document.getElementById('div1');
var oBtn=document.getElementsByTagName('input');
var bDiv=aDiv.getElementsByTagName('div');
for(var i=0;i<oBtn.length;i++)
{
//切换标签颜色 Begin
oBtn.index=i; // A
oBtn.onclick=function () {
for(var i=0;i<oBtn.length;i++) //运行正常,发现i的定义很奇怪
{
bDiv.style.display='none';
oBtn.className='';//class'N'ame
}
this.className='active';
bDiv.style.display='block'; //A
};//切换 End
};
};
</script>
</head>
<body>
<div id="div1">
<input type="button" value="进口" class='active'>
<input type="button" value="出口">
<input type="button" value="顺差">
<div style="display:block;">123</div>
<div>234</div>
<div>345</div>
</div>
</body>
</html>
(function(i){})(i)
的函数
重新翻看了闭包内容,重新了解了一下概念,感谢!<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
.active{
background-color:#796ABF;
}
#div1 div{
width:200px;
height: 200px;
background: #CCC;
border:1px solid #000 ;
display: none;
}
</style>
<script type="text/javascript">
window.onload=function(){
var aDiv=document.getElementById('div1');
var oBtn=document.getElementsByTagName('input');
var bDiv=aDiv.getElementsByTagName('div');
for(var i=0;i<oBtn.length;i++)
{
//切换标签颜色 Begin
(function(index){
oBtn[index].onclick=function () {
for(var j=0;j<oBtn.length;j++) //运行正常,发现i的定义很奇怪
{
bDiv[j].style.display='none';
oBtn[j].className='';//class'N'ame
}
this.className='active';
bDiv[index].style.display='block'; //A
};//切换 End
})(i);
}
};
</script>
</head>
<body>
<div id="div1">
<input type="button" value="进口" class='active'>
<input type="button" value="出口">
<input type="button" value="顺差">
<div style="display:block;">123</div>
<div>234</div>
<div>345</div>
</div>
</body>
</html>
楼主可以看看js闭包的内容