day-11.5 JS里面document元素对象自带属性classlist;

1 <body>
2         <div id="wrap" class="haha hehe xixi "></div>
3         
4         <script>
5             var oWrap = document.getElementById("wrap");
6                 console.log(oWrap.classList); //显示的是classname的伪数组;
7         </script>
8 </body>

元素对象.classList 属性可以读取元素对象的类名,classList是一个伪数组,支持的比较常用的方法有:

add; 对元素添加一个类名;

1 oWrap.classList.add ("haha"); //添加一个类名;

contains:查看classList中是否有该类名,通过返回值判断;

1 console.log(oWrap.classList.contains("pp")); //返回false,没有该类名;

 

forEach;遍历

remove:移除一个类名;

1 oWrap.classList.remove("haha","hehe");//移除这2个类名;

toggle:有这个类名就删除,没有则添加;

1 oWrap.classList.toggle("gg");//不支持同时传多个参数;

 

posted @ 2018-06-19 15:59  bibiguo  阅读(220)  评论(0)    收藏  举报