JavaScript Advanced Programming---classlist properties

Source: Internet
Author: User

1. Traditional methods:

When you manipulate the class name, you need to add, remove, and replace the class name with the ClassName property. As in the following example:

<div class= "BD user disabled" >...</div>

This div has a total of three class names, in order to delete a class name, you need to separate the three classes, and then processed, the process is as follows:

<script type= "Text/javascript" >        var classname=div.classname.split (/\s+/);        Find the class name to delete        var pos=-1,            I,len;        for (var i = 0; i < classname.length; i++) {            if (classname[i]== "user") {               pos=i;               break;            }        };        Classname.splice (i,1);        Div.classname=classname.join ("");//re-assemble the remaining class names    </script>

The above-mentioned aspects are traditional methods.

2, HTML5 new Method Classlist (), you can completely get rid of classname properties.

Specific use cases are as follows:

<! DOCTYPE html>
Note: However, the current classlist attribute is supported only by firefox3.6+ and Chrome.


JavaScript Advanced Programming---classlist properties

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.