一、classList API 是什么

  1. 属于 DOM API,HTML5 引入,用来操作 HTML 标签的 class 属性值。
  2. classList 属性是一个只读的类数组对象,“实时”地代表了元素的类名集合。
  3. classList 对象上定义了 6 个实用的操作 class 属性值的方法。

二、classList 对象上的属性和方法

属性:

length:返回当前类列表中类的个数。

方法:

  1. add(class1, class2, ...):添加类
  2. remove(class1, class2, ...):删除类
  3. toggle(class[, force]):当前类列表中如果不存在指定的类,则执行添加类操作,返回 true;否则执行删除类操作,返回 false。force 是一个函数,决定 toggle 方法最终执行何种操作——当函数返回 true 时,执行添加类操作,返回 false,执行删除类操作。
  4. contains(class):是否包含指定类。
  5. item(index):返回当前类列表中指定位置的类名。
  6. 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)属于部分支持,不支持的内容包括:

  1. 不支持 SVG 或 MathML 元素。
  2. 不支持 toggle 方法的第二个参数。
  3. 不支持多参数形式的 add()remove() 方法。

不过都是小事,可以使用 polyfill 解决。

参考链接

https://www.sitepoint.com/exploring-classlist-api/

(完)

使用 classList API的更多相关文章

  1. [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 ...

  2. HTML5 classList API接口

    原文地址:HTML5 classList API 原文日期: 2010年07月13日 翻译日期: 2013年08月23日 当我陷入JavaScrip和JavaScript类库框架之中时,我总是有种希望 ...

  3. 温习classList api

    有道题是一个removeClass的功能,代码里是正则分隔了传入的name,根据name的个数,循环移除掉,让寻找bug..看了了这几行代码,首先想到的是我会如何去优化. 如果看代码一两分钟就能找到公 ...

  4. HTML5 classList API

    Having thrust myself into the world of JavaScript and JavaScript Libraries, I've often wondered: Whe ...

  5. [转]HTML5 classList API

    Having thrust myself into the world of JavaScript and JavaScript Libraries, I've often wondered: Whe ...

  6. HTML5的classList API优化对样式名className的操作

    //添加一个class elem.classList.add(classname); //删除一个class elem.classList.remove(classname); //判断一个class ...

  7. element.dataset API

    不久之前我向大家展示了非常有用的classList API,它是一种HTML5里提供的原生的对页面元素的CSS类进行增.删改的接口,完全可以替代jQuery里的那些CSS类操作方法.而另外一个非常有用 ...

  8. H5新增属性classList

    H5新增属性classList h5中新增了一个classList,原生js可以通过它来判断获取dom节点有无某个class. classList是html元素对象的成员,它的使用非常简单,比如 co ...

  9. h5的classList对象

    H5新增属性classList h5中新增了一个classList,原生js可以通过它来判断获取dom节点有无某个class. classList是html元素对象的成员,它的使用非常简单,比如 co ...

随机推荐

  1. centos6下搭建gitlab

    gitlab安装方法,最新安装方法见官网:https://www.gitlab.com.cn/installation/#centos-6 1.在 Centos 6 系统上, 下面的命令将在系统防火墙 ...

  2. Hat’s Words(字典树的运用)

    个人心得:通过这道题,对于树的运用又加深了一点,字典树有着他独特的特点,那个指针的一直转换着实让我好生想半天, 不得不佩服这些发明算法人的大脑. 这题的解决方法还是从网上找到的,还好算法是自己实现得, ...

  3. django的related_name

    转:https://segmentfault.com/q/1010000003705677 就是一个反向关联的属性,比方说model里面定义两个class,一个是A,一个是B class A(Mode ...

  4. Python函数-cmp()

    cmp(x, y) 作用:         比较两个对象x和y,如果x < y ,返回负数:x == y, 返回0:x > y,返回正数. 注:在python2所有版本中都可用,但在pyt ...

  5. 解决mac下sublime中文乱码

    Mac OS X 属于 Apple 独家演绎的 Unix 分支版本,默认使用 UTF-8 编码,当使用不同开发平台的小伙伴们,共同维护一份代码的时候,尤其现在很多人都还在用 Windows 系统的时候 ...

  6. bzoj 3527 [Zjoi2014]力——FFT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3527 把 q[ i ] 除掉.设 g[ i ] = i^2 ,有一半的式子就变成卷积了:另一 ...

  7. Dockerfile 部署应用执行脚本文件

    FROM centos6.6:0.0.1 MAINTAINER syberos:wangmo RUN mv /etc/yum.repos.d/ /etc/yum.repos.d_bak/ && ...

  8. 使用navicat进行数据表迁移

    使用navicat进行数据和表迁移只需要复制,粘贴就可以实现.

  9. GWT 中实现“CSS Sprite”

    近段时间在弄GWT这一块,开发中遇到的一些不错的方法或者技巧,在此做个分享和记录,有不同见解可发表意见  互相切磋. 在web开发中,必然涉及到网页中的图片,本地浏览网页,要下载在服务器端的图片,然后 ...

  10. PostgreSQL 监控磁盘使用

    监控磁盘使用 1. 判断磁盘用量 每个表都有一个主要的堆磁盘文件,大多数数据都存储在其中.如果一个表有着可能会很宽(尺寸大)的列, 则另外还有一个TOAST文件与这个表相关联, 它用于存储因为太宽而不 ...