使用 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 ...
随机推荐
- HihoCoder1415后缀数组三·重复旋律3
重复旋律3 时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为长度为 N 的数构成的数列.小Hi在练习过很多 ...
- VI编辑器、ipython、jupyter及进程
VI编辑器.ipython.jupyter及进程知识总结 https://www.cnblogs.com/thoughtful-actors/p/9650959.html VI编辑器.ipython. ...
- ajax返回
1.几种方式public function getAjax(){ //$data = 'ok'; //$this->ajaxReturn($data); // 'ok' //$this-> ...
- JavaWeb框架_Struts2_(四)----->表达式语言OGNL
2. 表达式语言OGNL 2.1 OGNL简介 OGNL(Object-Graph Navigation Language)对象图导航语言的缩写,OGNL是一种表达式语言(Expression L ...
- angular : copy vs extend
While using AngularJS, we come across some situation in which we need to copy one object to another ...
- Python 函数之lambda、map、filter和reduce
1.lambda函数 lambda()是Python里的匿名函数,其语法如下: lambda [arg1[, arg2, ... argN]]: expression 学习条件运算时,对于简单的 if ...
- C# winfrom FastReport 变量设计加载
1.源码 DataTable dt5 = new DataTable(); dt5 = SqlHelper.SqlGetDataTable(StrSql, "tbEmpCont") ...
- mycat 新增分片和字符集
执行 select * from travelrecord ,分析Debug日志,说明整个执行逻辑,包括连接获取,连接同步信息,数据合并,数据返回,连接释放 新增一个分片表 T_VOTE (ID,PR ...
- 【转】href="#"与"javascript:void(0);"的区别
在工作中,如果我们想把a标签中的链接置成空链接,我们一般会用两种方法: 1 <a href="#" target="_blank"></a&g ...
- PowerDesigner生成sql脚本时去掉双引号并把字段名设为大写
Database菜单—Edit Current RDBMS 找到Script---sql—Format--- CaseSensitivityUsingQuote,把它设置为NO 这样再用sql pre ...