Talking about Classlist

Source: Internet
Author: User

Earlier we have been exposed to the hassclass,addclass,removeclass of jquery and other operations on the class but sometimes we need to apply to the original things, at this time classlist will be a great glory;

Personal understanding: Classlist is like a collection of class names, class +list represents the list of classes, and since it is the use of classes, we will introduce the members of the Classlist:

Length

static properties. Gets the number of element class names: Var Number=document.body.classlist.length;

Item

Gets the class name of the element, accepting a parameter, the numeric index value: var name=docuemnt.body.classlist.item (index);

Add

class for adding elements, accepting a parameter (class name) Note: You can only add one class name at a time: Document.body.classList.add ("classname");

Remove

Delete the element class, accept an argument (only one at a time): Document.body.classList.remove (' classname ');

Contains

Detects whether a class is included and returns a Boolean value. Document.body.classList.contains (' classname ');//true or False

Toggle

The equivalent of add remove contains, the existence of the class on the Remove (), does not exist to delete the Add ();d ocumen.body.classList.toggle (' classname ');

Talking about Classlist

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.