HTML5 classList API
Having thrust myself into the world of JavaScript and JavaScript Libraries, I've often wondered:
When are browser vendors going to see the helper methods/libraries created by the JavaScript toolkits and implement these functionalities natively within the browser? I realize that standards are important and browser vendors can't afford to
half-ass these implementations but I do believe they could be...expedited. The good news is that one of these functionalities has been add to the HTML5 API;
classList.
The classList object, added to all nodes within the DOM, provides developers methods by which to add, remove, and toggle CSS classes on a node. classList also allows developers to check if a CSS
class has been assigned to a given node.
Element.classList
The classList
object contains a number of helpful methods:
{
length: {number}, /* # of class on this element */
add: function() { [native code] },
contains: function() { [native code] },
item: function() { [native code] }, /* by index */
remove: function() { [native code] },
toggle: function() { [native code] }
}
Element.classList, as you can see, is a small but useful collection of methods.
Adding a CSS Class
The add method allows you to add one more multiple space-separated classes:
myDiv.classList.add('myCssClass');
Removing a CSS Class
The add method allows you to remove a single class:
myDiv.classList.remove('myCssClass');
You could separate multiple classes by space but the result may not be incredibly reliable.
Toggling a CSS Class
myDiv.classList.toggle('myCssClass'); //now it's added
myDiv.classList.toggle('myCssClass'); //now it's removed
Note: If toggle is called and the element does not have the provided CSS class, the class is added.
Contains CSS Class Check
myDiv.classList.contains('myCssClass'); //returns true or false
The classList API is now supported by all modern browsers, so look for the JavaScript libraries to include
classList checks instead of parsing an element's class attribute!
HTML5 classList API的更多相关文章
- HTML5 classList API接口
原文地址:HTML5 classList API 原文日期: 2010年07月13日 翻译日期: 2013年08月23日 当我陷入JavaScrip和JavaScript类库框架之中时,我总是有种希望 ...
- [转]HTML5 classList API
Having thrust myself into the world of JavaScript and JavaScript Libraries, I've often wondered: Whe ...
- 温习classList api
有道题是一个removeClass的功能,代码里是正则分隔了传入的name,根据name的个数,循环移除掉,让寻找bug..看了了这几行代码,首先想到的是我会如何去优化. 如果看代码一两分钟就能找到公 ...
- 使用 classList API
一.classList API 是什么 属于 DOM API,HTML5 引入,用来操作 HTML 标签的 class 属性值. classList 属性是一个只读的类数组对象,"实时&qu ...
- HTML5 程序设计 - 使用HTML5 Canvas API
请你跟着本篇示例代码实现每个示例,30分钟后,你会高喊:“HTML5 Canvas?!在哥面前,那都不是事儿!” 呵呵.不要被滚动条吓到,很多都是代码和图片.我没有分开写,不过上面给大家提供了目录,方 ...
- HTML5 Drop API
转自:http://www.cnblogs.com/fsjohnhuang/p/3961066.html 一.前言 在HTML4的时代,各前端工程师为了实现拖拽功能可说是煞费苦心,初听HTML5 ...
- Three.js + HTML5 Audio API 打造3D音乐频谱,Let’s ROCK!
继续玩味之前写的音乐频谱作品,将原来在Canvas标签上的 作图利用Three.js让它通过WebGL呈现,这样就打造出了一个全立体感的频谱效果了. 项目详情及源码 项目GitHub地址:https: ...
- HTML5 File API — 让前端操作文件变的可能
前言 在 HTML5 File API 出现之前,前端对于文件的操作是非常有局限性的,大多需要配合后端实现.出于安全角度考虑,从本地上传文件时,代码不可能获取文件在用户本地的地址,所以纯前端不可能完成 ...
- 开大你的音响,感受HTML5 Audio API带来的视听盛宴
话说HTML5的炫酷真的是让我爱不释手,即使在这个提到IE就伤心不完的年代.但话又说回来,追求卓越Web创造更美世界这样高的追求什么时候又与IE沾过边儿呢?所以当你在看本文并且我们开始讨论HTML5等 ...
随机推荐
- spring中使用quartz时注入时出现的错误
错误1: 配置文件: <!-- 任务执行器的线程池 --> <bean id="taskExecutor" class="org.springframe ...
- 架构实战项目心得(十四):spring-boot结合Swagger2构建RESTful API测试体系
一.添加依赖: <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-s ...
- 【关于迭代器的for-each遍历集合现象。。。。。】
foreahc迭代集合元素的同时修改集合元素抛异常..ConcurrentModificationException异常 只要使用迭代器遍历,其他集合遍历时进行增删操作都需要留意是否会触发Concur ...
- 借助 CORS 从 JavaScript 使用 API 应用
应用服务提供内置的跨域资源共享 (CORS) 支持,可让 JavaScript 客户端对 API 应用中托管的 API 进行跨域调用.应用服务允许配置对 API 的 CORS 访问,无需在 API 中 ...
- [android] 切换界面的通用处理
实现不改变activity,只切换View 抽取View界面的基类 利用面向对象多态的思路,实现通用 TitleManager.java 管理标题 package com.tsh.lottery.vi ...
- 4、springboot之全局异常捕获
1.新建一个springboot项目 添加一个全局异常的类 import org.springframework.web.bind.annotation.ControllerAdvice; impor ...
- 三、Bean的初始化
一.使用构造器实例化Bean:这是最简单的方式,Spring IOC容器既能使用默认空构造器也能使用有参构造器两种方式创建bean 空构造器 <bean name="bean1&quo ...
- JDK自带工具keytool生成ssl证书 和 HTTPS双向认证
创建证书(第一步) keytool -genkey -alias "baidu" -keypass "123456" -keystore "D:/ba ...
- 给div加上padding和border,如何不让div整体改变
最近要入门H5,遇到很多困惑,所以,每解决一个,我就要写在博客里,以防忘记! 给div加上padding和border,如何不让div整体改变? 如果想要实现这样的效果,只需要在这个div块中写入 b ...
- Canvas中的save方法和restore方法
初学者也许会误认为canvas中save方法是用来保存绘图状态的图形,而restore方法是用来还原之前保存的绘图状态的图形,其实不然. save():保存当前的绘图状态. restore():恢复之 ...