JavaScript's classname operation method is uncomfortable, he gets a continuous stringMust be split apart with split, so many people want to use jquery Addclass,removeclass,hasclass and so on.Classlist solves this problem, although compatibility is not good. But it's going to be better, let's learn first.HtmlElement will have classlist properties,The main methods are as follows:El.classList.add ()El.classLis
Use the classList in html5.
After years of competition in the JavaScript and JavaScript tool libraries, I often have this dream: When can modern browsers provide helper methods and class libraries to replace those JavaScript tool libraries, such as jQuery, let's replace them with the native method of the browser. I know that browsers will definitely improve in this direction, but this evolution process will
Prior to this, jquery hasclass, AddClass, removeclass we are familiar with, but we do not in each project will be used jquery or zepto, for example, in the mobile end of the page, In view of the legendary performance and static resource requirements, we usually choose to use the original JS, but for the element class operation. The first thing you think about is classname, the guy who got the support of all
We have introduced some new HTML5 selectors, including querySelector (), querySelectorAll (), and getElementsByClassName (). These three features have their own unique functions. If you need them, you can refer to the related content in HTML5 practice and analysis. Today, we will introduce the classList attribute.
What is the classList attribute? let's leave it alone. To solve this problem, how can we delet
object: A read-only class array object . Referring to the array of classes, you will certainly think of Array.prototype.XX.call (e.classlist) to call the array method. However, the methods defined by Domtokenlist are more practical than the array elements.1. Add() adds a class name to the class attribute of the element.2.Remove () clears a class name from the class attribute of the element.3.Toggle () indicates that an element is added if it does not exist, otherwise it is deleted. Very practic
Before HTML5 the new classlist, the class of the operation element was classname this attribute, and if you want to encapsulate hasclass, removeclass, addclass for jquery, Toggleclass as elegant operation calssname, in the past we have to classname package processing, as follows, The following code you may not be able to understand the running process, but should be able to understand the functional section
A pair of classlist overall feelBrowser support Reference (http://www.runoob.com/jsref/prop-element-classlist.html) Classlist is an object that returns a value: A domtokenlist that contains the list of class names for the elementSyntax:element. classlisthas an attribute length read-onlyMethod:1.add (Class1, Class2, ...) add one or more class names to the element. If the specified class name already exists,
Preface
If you think you want to write some classes, you will find your own shortcomings. Actually, it's not how you implement a class, but how you design a class to make it easier for developers to operate. For this operation style, you can access the style through javascript. However, in javascript advanced programming, CSS, javascript, and HTML have a high Coupling Degree and are not suitable for maintenance. In addition, className, however, we know that className is a string that can be read
In the HTML5 API, each node in the page Dom has an classList object that the programmer can use to add, delete, and modify CSS classes on the node. classList, programmers can also use it to determine whether a node has been given a CSS class.First, Element.classlistclassListThere are a number of useful methods in this object:1 {2 Length: {number},/* # of class on the This element */3 Add: Function () {[na
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
These two days to do a full selection of anti-election cases, using a number of methods, just beginning to write the stumbling, listen to the teacher after explaining the idea, then to write it is very easy. In fact, it is a question of thinking when writing code. The first thing to do is to analyze the function, then step it up and not mix it up. The following methods for a more streamlined method, using the Classlist attribute, just beginning to wri
This article is mainly to share with you CSS using classlist to achieve two button style switching effect, on some pages we need to use two buttons to switch back and forth, how to achieve such a function? Friends who need to follow the script of the small to learn together.
Method of Classlist Property: Add (); remove (); toggle ();
Description, on some pages we need to switch back and forth using two but
Set the DOM element class is we are very easy to encounter in the project, the online information and summary is more, the following more comprehensive collation, I hope to give you some help! and reference to two mature classlist compatibility methodsOne, El.setattribute (' class ', ' abc ');var div = document.getElementById (' D1 ');Div.setattribute ("Class", "abc");Compatible: Ie8/9/10/firefox/safari/chrome/opera support IE6/7 does not support seta
Classlist property What exactly is doing, we first leave classlist regardless. Let's consider the question of how do we delete one of the class names in an element that has more than one class name? Dream Dragon rivalry brains son finally thought of a method of realization. The class name in the class name Li, Meng, and long, three class names will be Meng deleted. The code is as followsHTML code
V
Each node in the page Dom has an classList object that the programmer can use to add, delete, and modify CSS classes on the node. classList, programmers can also use it to determine whether a node has been given a CSS class.Adding a Class (add)document.getElementById ("Mydiv"). Classlist.add ("MyStyle");To add multiple classes for the document.getElementById ("Mydiv"). Classlist.add ("MyStyle", "Anotherclas
Javascript advanced programming --- classList attributes
1. Traditional methods:
You must use the className attribute to add, delete, and replace Class names. For example:
...
There are three class names in this div. To delete one class name from the class name, you need to split the three classes and then process them separately. The process is as follows:
The above is a traditional method.
2. The new method
In the HTML5 API, each node in the page Dom has an classList object that the programmer can use to add, delete, and modify CSS classes on the node. classList, programmers can also use it to determine whether a node has been given a CSS class.Element.classlistclassListThere are a number of useful methods in this object:{ /** /add:function () {[native code]}, contains:function () {[Native code]},
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: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:The above-mentioned aspects are traditional methods.2, HTML5 new Method Classlist (), you can completely get rid of classname
PrefaceFeel like you're going to write some classes, and you'll really find yourself in the wrong place. Actually, it's not that you implement a class, but how you design a class that makes it easier for developers to manipulate. For this style of operation, JavaScript can be used to access the style, but in the JavaScript advanced programming, it is said that CSS, JavaScript, HTML coupling is too high, not suitable for maintenance, and through classname, But we know that ClassName is a develope
This article brings you the content is about HTML5 new properties: The use of classlist attributes, there is a certain reference value, a friend can refer to the need, I hope you have some help.
The Classlist attribute is added to the HTML5 to implement the class operation with the following API:
Contains (value): Determines whether the class name named value exists Add (value): Add a class name named val
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.