使用 classList API
一、classList API 是什么
- 属于 DOM API,HTML5 引入,用来操作 HTML 标签的
class
属性值。 classList
属性是一个只读的类数组对象,“实时”地代表了元素的类名集合。classList
对象上定义了 6 个实用的操作class
属性值的方法。
二、classList
对象上的属性和方法
属性:
length
:返回当前类列表中类的个数。
方法:
add(class1, class2, ...)
:添加类remove(class1, class2, ...)
:删除类toggle(class[, force])
:当前类列表中如果不存在指定的类,则执行添加类操作,返回true
;否则执行删除类操作,返回false
。force 是一个函数,决定toggle
方法最终执行何种操作——当函数返回true
时,执行添加类操作,返回false
,执行删除类操作。contains(class)
:是否包含指定类。item(index)
:返回当前类列表中指定位置的类名。toString()
:返回当前类列表的字符串表示。
接下来会举些使用上述方法和属性的例子(这里有一个 线上的例子 ),这些例子都会用到下面这段 HTML 代码。
<span id="element" class="description"></span>
2.1 add()
方法
添加一个类名:
document.getElementById('element').classList.add('red');
// class="description red"
添加多个类名:
document.getElementById('element').classList.add('red', 'bold');
// class="description red bold"
{提示} 如果提供的类名已存在,就不会重复添加。
2.2 remove()
方法
删除一个类名:
document.getElementById('element').classList.remove('description');
// class=""
删除多个类名:
document.getElementById('element').classList.remove('description', 'red');
// class=""
2.3 toggle()
方法
document.getElementById('element').classList.toggle('description');
// class=""
document.getElementById('element').classList.toggle('description');
// class="description"
2.4 item()
方法
document.getElementById('element').classList.item(0);
// 返回 "description"
document.getElementById('element').classList.item(2);
// 返回 null
2.5 contains
方法
if (document.getElementById('element').classList.contains('description')) {
// 一些逻辑代码……
} else {
// 不同的逻辑代码……
}
2.6 toString()
方法
console.log(document.getElementById('element').classList.toString());
// 打印 "description"
document.getElementById('element').classList.add('red', 'bold');
console.log(document.getElementById('element').classList.toString());
// 打印 "description red bold"
2.7 length
属性
console.log(document.getElementById('element').classList.length);
// 打印 1
浏览器兼容性
IE10+ 和其他所有浏览器。其中 IE10+(包括土鳖 UC)属于部分支持,不支持的内容包括:
- 不支持 SVG 或 MathML 元素。
- 不支持
toggle
方法的第二个参数。 - 不支持多参数形式的
add()
和remove()
方法。
不过都是小事,可以使用 polyfill 解决。
参考链接
https://www.sitepoint.com/exploring-classlist-api/
(完)
使用 classList API的更多相关文章
- [Javascript] Manipulate the DOM with the classList API
Learn how to add, remove and test for CSS classes using the classList API. It's more powerful than u ...
- HTML5 classList API接口
原文地址:HTML5 classList API 原文日期: 2010年07月13日 翻译日期: 2013年08月23日 当我陷入JavaScrip和JavaScript类库框架之中时,我总是有种希望 ...
- 温习classList api
有道题是一个removeClass的功能,代码里是正则分隔了传入的name,根据name的个数,循环移除掉,让寻找bug..看了了这几行代码,首先想到的是我会如何去优化. 如果看代码一两分钟就能找到公 ...
- HTML5 classList API
Having thrust myself into the world of JavaScript and JavaScript Libraries, I've often wondered: Whe ...
- [转]HTML5 classList API
Having thrust myself into the world of JavaScript and JavaScript Libraries, I've often wondered: Whe ...
- HTML5的classList API优化对样式名className的操作
//添加一个class elem.classList.add(classname); //删除一个class elem.classList.remove(classname); //判断一个class ...
- element.dataset API
不久之前我向大家展示了非常有用的classList API,它是一种HTML5里提供的原生的对页面元素的CSS类进行增.删改的接口,完全可以替代jQuery里的那些CSS类操作方法.而另外一个非常有用 ...
- H5新增属性classList
H5新增属性classList h5中新增了一个classList,原生js可以通过它来判断获取dom节点有无某个class. classList是html元素对象的成员,它的使用非常简单,比如 co ...
- h5的classList对象
H5新增属性classList h5中新增了一个classList,原生js可以通过它来判断获取dom节点有无某个class. classList是html元素对象的成员,它的使用非常简单,比如 co ...
随机推荐
- freemarker实现第一个HelloWorld
第一步:引入freemarker jar包 第二步:创建templates下的test01.ftl 第三步:在web.xml下 第四步:编写后台代码 package com.wisezone.test ...
- Python 2.7_多进程获取简书专题数据(一)
学python几个月了正好练练手,发现问题不断提高,先从专题入手,爬取些数据,一开始对简书网站结构不熟悉,抓取推荐,热门,城市3个导航栏,交流发现推荐和热门是排序不同,url会重复,以及每个专题详情页 ...
- UV有问题?
1.检查读取显示贴图的环境与制作贴图环境UV坐标系是否一致. 如:Directx左上角(0,0),右下角(1,1) unity 左下角(0,0),右上角(1,1) 两者互转需要垂直镜像.
- Vue forms
Vue forms Vue 的表单. 表单中的数据和是双向绑定的. 你可以使用 v-model 对控件元素进行数据双向绑定. 比较有用的修饰符 .lazy .number .trim
- 新的开发域名 fastadmin.tk
新的开发域名 fastadmin.tk 这个的所有子域名批向 127.0.0.1,可以用于开发. 以后不用再修改系统的 hosts. 使用案例 手把手教你安装 FastAdmin 到虚拟主机 (php ...
- windbg调试实例(4)--句柄泄露
同事介绍了一篇调试句柄泄露的blog文章,今天有空看了一下,这家伙用视频的方式录下整个调试的过程,学习一目了然,真是有心.鉴于学习的过程总结一下能加深记忆,所以我这里做个记录,感兴趣的朋友可以看这里: ...
- BZOJ2120:数颜色(莫队版)
浅谈莫队:https://www.cnblogs.com/AKMer/p/10374756.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php?i ...
- Windows命令查看文件的MD5/SHA1/SHA256
certutil -hashfile "D:\Tools\Microsoft\SqlServer\2016\ct_sql_server_2016_enterprise_x64_dvd_869 ...
- spark 算子分析
别的不说先上官网: action 这些算子中需要注意: 1.reduce 和 reduceByKey 虽说都有reduce,但是一个是action级别,一个是transformation级别,速度上会 ...
- &(((struct A*)NULL)->m_float)---offsetof
问题描述: struct A { int m_int; float m_float; }; int main(void) { printf("%p",&(((struct ...