Fork me on GitHub

原生js增加,移除类名

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .box{width: 200px;height: 200px;background: yellowgreen}
        .page{color: red}
        .btn{font-size: 20px;}
    </style>
</head>
<body>

<div class="box">这是测试</div>

<script>
var box=document.getElementsByClassName("box")[0];

        box.className=box.className+" "+"page";//增加类名
        box.classList.add("btn");//增加类名
        box.classList.remove("btn");//移除类名
        box.classList.item(1);//根据索引找类名
        console.log(box.className);
        console.log(box.classList.item(1))

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

posted @ 2017-09-04 15:13  小白不白10  阅读(1001)  评论(0编辑  收藏  举报