求助!!!!!我写的代码,不知道怎么搞的,就是不成功,会出现点击一下就出现一瞬间的现象,也不知道什么原因,各位大侠帮我看看,

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

feibenren

2015-04-13 10:08

<!doctype html>

<html>

<head>

<meta charset="UTF-8">

<title>tab切换 模仿</title>

<style type="text/css">

*{margin: 0;padding: 0;}

li{list-style: none;}

a{text-decoration: none;}

.wrapper{border: 1px solid #aaa;width: 250px;margin: 0 auto;position: relative;overflow: hidden;height: 100px;overflow: hidden;}

.tit{height: 30px;width: 258px;position: absolute;left: -1px;overflow: hidden;background-color: #eee;}

.tit a{display: block;line-height: 30px;background-color: #eee;

width: 50px;text-align: center;float: left;

color: #000;

margin-top: -1px;


border-bottom: 1px solid #aaa;

 }

.tit a:hover{background-color: #fff;color: orange;

border-left:1px solid #aaa;border-right:1px solid #aaa;

border-bottom: 1px solid #fff;}

.con{float: left;height: 70px;width: 250px; position: absolute;top:30px;}

.con .detail{display: none;float: left;padding: 10px;}

.con ul li{display: block;float: left;margin: 3px 7px;}

.show{display: block!important;}

</style>

</head>

<body>

<div>

<div id="tit">

<a href="">公告</a>

<a href="">规则</a>

<a href="">论坛</a>

<a href="">安全</a>

<a href="">公益</a>

</div>

<div id="con">

<ul  class="detail">

<li>公告公告公告</li>

<li>公告公告公告</li>

<li>公告公告公告</li>

<li>公告公告公告</li>

</ul>


<ul  class="detail ">

<li>规则规则规则</li>

<li>规则规则规则</li>

<li>规则规则规则</li>

<li>规则规则规则</li>

</ul>


<ul>

<li>论坛论坛论坛</li>

<li>论坛论坛论坛</li>

<li>论坛论坛论坛</li>

<li>论坛论坛论坛</li>

</ul>


<ul>

<li>安全安全安全</li>

<li>安全安全安全</li>

<li>安全安全安全</li>

<li>安全安全安全</li>

</ul>


<ul>

<li>公益公益公益</li>

<li>公益公益公益</li>

<li>公益公益公益</li>

<li>公益公益公益</li>

</ul>

</div>

</div>

</body>

</html>


<script type="text/javascript">

function $(id){

return document.getElementById(id);

}

var tits=$("tit").getElementsByTagName('a');

var cons=$("con").getElementsByTagName('ul');

// cons[1].className="detail show";

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

tits[i].onclick=function change(){

cons[0].className="detail show";

}

};


</script>


写回答 关注

1回答

  • qingqinxl01
    2015-04-13 11:38:43
    已采纳

    <!doctype html>


    <html>


    <head>


    <meta charset="UTF-8">


    <title>tab切换 模仿</title>


    <style type="text/css">


    *{margin: 0;padding: 0;}


    li{list-style: none;}


    a{text-decoration: none;}


    .wrapper{border: 1px solid #aaa;width: 250px;margin: 0 auto;position: relative;overflow: hidden;height: 100px;overflow: hidden;}


    .tit{height: 30px;width: 258px;position: absolute;left: -1px;overflow: hidden;background-color: #eee;}


    .tit a{display: block;line-height: 30px;background-color: #eee;


    width: 50px;text-align: center;float: left;


    color: #000;


    margin-top: -1px;




    border-bottom: 1px solid #aaa;


     }


    #tit a{cursor:pointer}

    #tit a:hover{background-color: #fff;color: orange;


    border-left:1px solid #aaa;border-right:1px solid #aaa;


    border-bottom: 1px solid #fff;}


    #con{float: left;height: 70px;width: 250px; position: absolute;top:30px;}


    #con .detail{display: none;float: left;padding: 10px;}


    #con ul li{display: block;float: left;margin: 3px 7px;}


    .show{display: block!important;}


    </style>


    </head>


    <body>


    <div>


    <div id="tit">


    <a>公告</a>


    <a>规则</a>


    <a>论坛</a>


    <a>安全</a>


    <a>公益</a>


    </div>


    <div id="con">


    <ul>


    <li>公告公告公告</li>


    <li>公告公告公告</li>


    <li>公告公告公告</li>


    <li>公告公告公告</li>


    </ul>




    <ul  class="detail ">


    <li>规则规则规则</li>


    <li>规则规则规则</li>


    <li>规则规则规则</li>


    <li>规则规则规则</li>


    </ul>




    <ul class="detail">


    <li>论坛论坛论坛</li>


    <li>论坛论坛论坛</li>


    <li>论坛论坛论坛</li>


    <li>论坛论坛论坛</li>


    </ul>




    <ul class="detail">


    <li>安全安全安全</li>


    <li>安全安全安全</li>


    <li>安全安全安全</li>


    <li>安全安全安全</li>


    </ul>




    <ul class="detail">


    <li>公益公益公益</li>


    <li>公益公益公益</li>


    <li>公益公益公益</li>


    <li>公益公益公益</li>


    </ul>


    </div>


    </div>


    </body>


    </html>




    <script type="text/javascript">


    function $(id){


    return document.getElementById(id);


    }


    var tits=$("tit").getElementsByTagName('a');


    var cons=$("con").getElementsByTagName('ul');


    // cons[1].className="detail show";

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

    var bindClick = function(i){

    tits[i].onclick=function change(){

    hideAll();

    cons[i].className="detail show";

    return false;

    }

    };

    bindClick(i);

    };

    function hideAll(){

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

    cons[i].className = "detail";

    };

    }




    </script>

    有三处错误:

    1、样式表中的tit和con选择器书写错误;

    2、循环事件绑定的写法拿到的值永远都是i=5

    3、默认第一个ul展示,其他的都为隐藏;另外在点击第当前ul的时候需要把之前显示的ul的display置为none

    feiben...

    非常感谢!

    2015-04-20 18:47:31

    共 1 条回复 >

Tab选项卡切换效果

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

65466 学习 · 575 问题

查看课程

相似问题