jquery——选项卡

下面是闭包做选项卡:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>闭包做选项卡</title>
    <style type="text/css">
        .btns{
            width:500px;
            height:50px;
        }
        .btns input{
            width:100px;
            height:50px;
            background-color: #ddd;
            color:#666;
            border:0;
        }
        .btns input.cur{
            background-color: gold;
        }

        .contents div{
            width:500px;
            height:300px;
            background-color: gold;
            display: none;
            line-height:300px;
            text-align: center;
        }

        .contents div.active{
            display: block;
        }

    </style>
    <script type="text/javascript">

        window.onload = function(){

            var aBtn = document.getElementById('btns').getElementsByTagName('input');

            var aContent = document.getElementById('contents').getElementsByTagName('div');

            //用闭包存起来,这个i就有1,2,3这个值了,不过实际中不这样用,小题大做了
            for(var i=0;i<aBtn.length;i++){

                (function (i) {
                    aBtn[i].onclick = function () {

                        for(var j=0;j<aBtn.length;j++){
                            aBtn[j].className = '';
                            aContent[j].className = '';
                        }

                        this.className = 'cur';
                        aContent[i].className = 'active';
                    }
                })(i)
            }
        }

    </script>
</head>
<body>
    <div class="btns" id="btns">
        <input type="button" value="tab01" class="cur">
        <input type="button" value="tab02">
        <input type="button" value="tab03">
    </div>
    <div class="contents" id="contents">
        <div class="active">tab文字内容一</div>
        <div>tab文字内容二</div>
        <div>tab文字内容三</div>
    </div>

</body>
</html>

jquery库做选项卡:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>闭包做选项卡</title>
    <script type="text/javascript" src="jquery-1.12.4.min.js"></script>
    <style type="text/css">
        .btns{
            width:500px;
            height:50px;
        }
        .btns input{
            width:100px;
            height:50px;
            background-color: #ddd;
            color:#666;
            border:0;
        }
        .btns input.cur{
            background-color: gold;
        }

        .contents div{
            width:500px;
            height:300px;
            background-color: gold;
            display: none;
            line-height:300px;
            text-align: center;
        }

        .contents div.active{
            display: block;
        }
    </style>
    <script type="text/javascript">
        $(function () {

            $('#btns input').click(function(){
                //this是原生的对象
                $(this).addClass('cur').siblings().removeClass('cur');
                //alert($(this).index())弹出索引值
                $('#contents div').eq($(this).index()).addClass('active').
          siblings().removeClass('active') }) }) </script> </head> <body> <div class="btns" id="btns"> <input type="button" value="tab01" class="cur"> <input type="button" value="tab02"> <input type="button" value="tab03"> </div> <div class="contents" id="contents"> <div class="active">tab文字内容一</div> <div>tab文字内容二</div> <div>tab文字内容三</div> </div> </body> </html>

猜你喜欢

转载自www.cnblogs.com/gaoquanquan/p/9205653.html