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
Some CSS properties may need to be vendor-prefixed.

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的更多相关文章

  1. HTML5 classList API接口

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

  2. [转]HTML5 classList API

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

  3. 温习classList api

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

  4. 使用 classList API

    一.classList API 是什么 属于 DOM API,HTML5 引入,用来操作 HTML 标签的 class 属性值. classList 属性是一个只读的类数组对象,"实时&qu ...

  5. HTML5 程序设计 - 使用HTML5 Canvas API

    请你跟着本篇示例代码实现每个示例,30分钟后,你会高喊:“HTML5 Canvas?!在哥面前,那都不是事儿!” 呵呵.不要被滚动条吓到,很多都是代码和图片.我没有分开写,不过上面给大家提供了目录,方 ...

  6. HTML5 Drop API

    转自:http://www.cnblogs.com/fsjohnhuang/p/3961066.html 一.前言    在HTML4的时代,各前端工程师为了实现拖拽功能可说是煞费苦心,初听HTML5 ...

  7. Three.js + HTML5 Audio API 打造3D音乐频谱,Let’s ROCK!

    继续玩味之前写的音乐频谱作品,将原来在Canvas标签上的 作图利用Three.js让它通过WebGL呈现,这样就打造出了一个全立体感的频谱效果了. 项目详情及源码 项目GitHub地址:https: ...

  8. HTML5 File API — 让前端操作文件变的可能

    前言 在 HTML5 File API 出现之前,前端对于文件的操作是非常有局限性的,大多需要配合后端实现.出于安全角度考虑,从本地上传文件时,代码不可能获取文件在用户本地的地址,所以纯前端不可能完成 ...

  9. 开大你的音响,感受HTML5 Audio API带来的视听盛宴

    话说HTML5的炫酷真的是让我爱不释手,即使在这个提到IE就伤心不完的年代.但话又说回来,追求卓越Web创造更美世界这样高的追求什么时候又与IE沾过边儿呢?所以当你在看本文并且我们开始讨论HTML5等 ...

随机推荐

  1. Centos7 ftp服务器搭建

    1.使用yum安装ftp服务端: yum install -y vsftpd 2.使用yum安装ftp客户端: yum install -y ftp.x86_64 3.开启ftp服务设置开机启动并查看 ...

  2. IT自由职业者的第一个月(上)——何去何从

         2016年8月11号,我终于走完所有流程,拿到了离职证明,5年的嵌入式驱动开发工程师的职业生涯就此结束.走离职流程期间,陆续有同事和朋友问我,离职后去哪儿,我都是告诉他们:“还没想好,离了再 ...

  3. YII2使用gii

    在 config/web.php 文件中会有以下配置代码开启该模块: $config['bootstrap'][] = 'gii'; $config['modules']['gii'] = [ 'cl ...

  4. sqlserver 自增ID插入指定数据(转)

    set identity_insert 表名 ON --允许对自增列Id插入指定数据 insert into table_name(Id,Name) values(1,'test') set iden ...

  5. Shiro - 关于session

    Shiro Session session管理可以说是Shiro的一大卖点. Shiro可以为任何应用(从简单的命令行程序还是手机应用再到大型企业应用)提供会话解决方案. 在Shiro出现之前,如果我 ...

  6. mac隐藏和显示隐藏文件

    显示:defaults write com.apple.finder AppleShowAllFiles -bool true隐藏:defaults write com.apple.finder Ap ...

  7. [编程] C语言变量和数据类型总结练习题

    练习题: 1) 如何用 printf() 输出 short.int.long 类型的整数,请举例说明. 2) 如何用 printf() 输出 float.double 类型的小数,请举例说明. 3) ...

  8. 如何删除eclipse中已经保存的svn密码

    一.打开eclipse--->点击Window--->点击Perference,打开eclipse配置,输入svn,然后点击svn,找到下方svn接口,查看下svn是什么类型的接口,如果是 ...

  9. BI简介

    一.BI简介 BI全称是business intelligence,直译过来就是商业智能.BI表示的是一个体系,一套完整的解决方案.主要用于数据的整合.分析.挖掘等,为帮助企业决策而提供如报表.预测分 ...

  10. @RequestBody与serialize()、serializeArray()、拼接Json 妙用总结

    @requestBody注解常用来处理content-type不是默认的application/x-www-form-urlcoded编码的内容, 比如说:application/json或者是app ...