js 选项卡、添加类名(需要查重)和移除类名(不需要查重)

选项卡


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="Keywords" content="关键字,关键词">
    <meta name="Description" content="描述和简介">
    <title>Title</title>
    <style type="text/css">
        *{margin:0;padding:0;}
        body,ul,li,ol,dl,dd,p,h1,h2,h3,h4,h5,h6{ margin:0;}
        a{text-decoration:none;}
        img{border:none;}
        ol,ul{list-style:none;}
        #box{
            position: relative;
            width: 360px;
            height: 580px;
            margin : 50px auto;
        }
        #box .pic{
            float: left;
            position: relative;
            width: 325px;
            height: 100%;
        }
        #box .pic img{
            display: none;
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
        }
        #box .pic img.show{
            display: block;
        }
        #box .txt{
            position: absolute;
            bottom: 0;
            left: 0;
            width: 325px;
            height: 30px;
            background: rgba(0,0,0,0.5);
            text-align: center;
            line-height: 30px;
            color: #fff;
            font-size: 14px;
        }
        #box .tab{
            float: left;
            width: 30px;
        }
        #box .tab ul li{
            width: 30px;
            height: 30px;
            margin-bottom: 2px;
            background: #000;
            line-height: 30px;
            text-align: center;
            font-size: 16px;
            font-weight: bold;
            cursor: pointer;
            color: #fff;
        }
        #box .tab ul li.active{
            background: #ff305c;
        }
        #box .tab ul li.aa{
            border: 5px solid yellowgreen;
        }
    </style>
</head>
<body>
    <div id="box">
        <div class="pic">
            <img class="show" src="images/g1.jpg" alt="">
            <img src="images/g2.jpg" alt="">
            <img src="images/g3.jpg" alt="">
            <img src="images/g4.jpg" alt="">
        </div>
        <p class="txt">可爱萌狗狗</p>
        <div class="tab">
            <ul>
                <li class="active aa">1</li>
                <li>2</li>
                <li>3</li>
                <li>4</li>
            </ul>
        </div>
    </div>

    <script>
        var aImg = document.querySelectorAll("#box .pic img"),
            aTab = document.querySelectorAll("#box .tab ul li"),
            oTxt = document.querySelector("#box .txt"),
            pArrTxt = ["可爱萌狗狗","萌萌小猫咪","呆萌小猫咪","愤怒小翠鸟"],
            length = aImg.length,
            index = 0;

        for(var i = 0; i < length; i++){
            aTab[i].ind = i;//自一定属性,存下索引的值
            aTab[i].onclick = function (){
                aImg[index].className = "";
                aTab[index].className = "";
                index = this.ind;
                aImg[index].className = "show";
                aTab[index].className = "active";
                oTxt.innerHTML = pArrTxt[index];
            };
        };

    </script>
</body>
</html>

添加类名(需要查重)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="Keywords" content="关键字,关键词">
    <meta name="Description" content="描述和简介">
    <title>Title</title>
    <style type="text/css">
        *{margin:0;padding:0;}
        body,ul,li,ol,dl,dd,p,h1,h2,h3,h4,h5,h6{ margin:0;}
        a{text-decoration:none;}
        img{border:none;}
        ol,ul{list-style:none;}
    </style>
</head>
<body>
    <!--
        addClass 添加类名
            1: 全部类名都要进行查重
            2: 空格是否要加上

    -->
    <div id="box" class="on on aa wrap dachui aa"></div>

    <script>
        var oBox = document.getElementById("box");


        addClass(oBox, "on on dachui on dachui wrap wrap on wrap aa dachui");

        //arr = ["on","on","dachui"];
        function addClass (ele , cName){
            //先把原先的类名和传进来的类名按照空格进行切割
            var arr1 = ele.className.split(" ");
            var arr2 = cName.split(" ");
            var arr3 = arr1.concat(arr2);
            console.log(arr3);

            /*for(var i = 0; i < arr3.length; i++){
                for(var j = i+1; j < arr3.length; j++){
                    if(arr3[i] == arr3[j]){
                        arr3.splice(j,1);//从第j个开始,删除一个(把第j个删除掉)
                    }
                };
            };*/
            for(var i = 0; i < arr3.length; i++ ){
                for(var j = arr3.length-1; j > i; j--){
                    if(arr3[i] == arr3[j]){
                        arr3.splice(j,1);
                    };
                };
            };
            ele.className = arr3.join(" ");
            console.log(arr3);
        };


    </script>
</body>
</html>

移除类名(不需要查重)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="Keywords" content="关键字,关键词">
    <meta name="Description" content="描述和简介">
    <title>Title</title>
    <style type="text/css">
        *{margin:0;padding:0;}
        body,ul,li,ol,dl,dd,p,h1,h2,h3,h4,h5,h6{ margin:0;}
        a{text-decoration:none;}
        img{border:none;}
        ol,ul{list-style:none;}
    </style>
</head>
<body>
    <div id="box" class="on wrap wrap aa dachui wrap dachui bb cc"></div>

    <script>
        var oBox = document.querySelector("#box");

        removeClass(oBox,"on dachui wrap bb");

        function removeClass(ele,cName){
            var arr1 = ele.className.split(" ");
            var arr2 = cName.split(" ");
            console.log(arr1);
            console.log(arr2);
            for(var i = 0; i < arr2.length; i++){
                for(var j = arr1.length-1; j >= 0; j--){
                    if(arr2[i] == arr1[j]){
                        arr1.splice(j,1);
                    }
                };
            };
            ele.className = arr1.join(" ");
        };
    </script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值