使用属性方法操作样式

    一、

   本人理解是对元素上的class属性进行操作

    格式 标签.classList.方法()

    // 重点 !!!

        //classList:操作类(标签上的class属性)名

        //length:返回指定元素的类型的个数

        //add():给指定元素添加指定的类名

        //remove():移除指定元素的指定的类名

        //toggle():切换指定元素的指定的类名,有则移除。无测添加

        //item() 根据序号获取对应的类名

        //contains():判断指定元素是否含有指定的类名

         

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

</head>

<style>

    .box2{

        color: red;

    }

</style>

<body>

    <div class="box main top nav">盒子</div>

    <!-- classList 只对类别有效 针对类别的方法 -->

   

    <script>

        // 重点 !!!

        //classList:操作类(标签上的class属性)名

        //length:返回指定元素的类型的个数

        //add():给指定元素添加指定的类名

        //remove():移除指定元素的指定的类名

        //toggle():切换指定元素的指定的类名,有则移除。无测添加

        //item() 根据序号获取对应的类名

        //contains():判断指定元素是否含有指定的类名

        var box1=document.querySelector(',box');

        console.dir(box1);

        console.log(box1.classList.length);

        box1.classList.add('box2')

        box1.classList.add('box2','info')

         

        box1.classList.remove('top')

        box.classList.toggle('nav') //移除  (有则移除)

        box1.classList.toggle('list')//添加  (无测添加)

       

        //根据指定的序号来获取类

        console.log(box1.classList.item(0))

       

        //进行判断该标签上的类是否有这个类名

        console.log(box1.classList.contains('main'))//true

        console.log(box1.classList.contains('qwe'))//false

    </script>

</body>

</html>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值