关于index问题。求指教

来源:1-4 js实现切换效果

慧女神

2016-05-11 12:36

<!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>我的选项卡</title>

<style type="text/css">

*{margin:0;padding:0;}

#box

{

width:275px;

height:220px;

margin:22px auto;

font-family:"微软雅黑";

font-size:15px;

overflow:hidden;

}


ul

{

border-bottom:2px solid red;

height:30px;

}


li

{

list-style-type:none;

    float:left;

width:80px;

line-height:30px;

border-top:2px solid #999;

border-left:1px solid #999;

border-right:1px solid #999;

text-align:center;

margin-left:6px;


}


a

{

text-decoration:none;

color:#000;

display:block;

line-height:30px;

text-align:center;

    

}

.show

{

clear:both;

border-left:1px solid #999;

border-right:1px solid #999;

border-bottom:1px solid #999;

background: #FFC ;


.active

{

background: #ffc;

border-top:2px solid red;

}


.hide

{

display:none;

}

</style>

<script type="text/javascript">

window.onload=function(){

var Head=document.getElementById("head");

   var Lists=Head.getElementsByTagName("li") ;

   var Box=document.getElementById("box"); 

   var oDiv=Box.getElementsByTagName("div") ; 

   

   for(var i=0;i<Lists.length;i++)

   {

  Lists[i].index = i;/*这一句和后面  oDiv[this.index].className = "show"; 有什么关系?为什我去掉Lists[i].index = i这一句话,oDiv[this.index].className = "show";这句话就失效了??*/

  Lists[i].onmouseover=function()

  {

 for(var i=0;i<Lists.length;i++)

 {

Lists[i].className="";  

 }

 this.className="active";

 

 

 for(var j=0;j<oDiv.length;j++)

 {

 oDiv[j].className="hide"; 

 }

          oDiv[this.index].className = "show"; 

  }  

   }

   

}

</script>

</head>


<body>

<div id="box">

    <ul id="head">

        <li  class="active">房产</li>

        <li>家居</li>

        <li>二手房</li>

    </ul>    

     <div class="show">

        <a href="#">275万购昌平邻铁三居 总价20万买一居</a>

        <a href="#">200万内购五环三居 140万安家东三环</a>

        <a href="#">北京首现零首付楼盘 53万购东5环50平</a>

        <a href="#">京楼盘直降00 50中信府 公园楼王现房</a>

     </div>

  

     <div class="hide">

        <a href="#">京楼盘直降5000 中信府 公园楼王现房</a>

        <a href="#">200万内购五环三居 140万安家东三环</a>

        <a href="#">北京首现零首付楼盘 53万购东5环50平</a>

        <a href="#">275万购昌平邻铁三居 总价2一居0万买</a> 

     </div> 

  

     <div class="hide">

        <a href="#">北京首现零首付楼盘 53万购东5环50平</a>

        <a href="#">200万内购五环三居 140万安家东三环</a>

        <a href="#">275万购昌平邻铁三居 总价20万买一居</a>

        <a href="#">京楼盘直降5000 中信府 公园楼王现房</a>

     </div>   

   

</div>


</body>

</html>


写回答 关注

1回答

  • 不是本人__
    2016-05-12 13:42:28
    已采纳

    建议看看这个,闭包的原因:http://www.cnblogs.com/songjum/p/4545800.html

    注意运用闭包的常见错误-->for(var i,len=xx.length;i<len;i++)循环;当我们所引用的自由变量为i时,由于i一直在内存中没有释放,所以函数每次alert(i)时,其值均为最终的i;

    慧女神

    非常感谢!

    2016-05-14 15:28:23

    共 2 条回复 >

Tab选项卡切换效果

本课程详细介绍网页页面中最流行常用的tab切换效果

65462 学习 · 581 问题

查看课程

相似问题