Home>Article>Web Front-end> How to change the class attribute in jquery
Jquery method to change the class attribute: 1. Use the [addClass()] method to add the specified class name to the element matched; 2. Use the [removeClass()] method to remove the style; 3. Use [hasClass() )] method contains a certain style.
The operating environment of this tutorial: windows7 system, jquery3.2.1 version. This method is suitable for all brands of computers.
How to change the class attribute in jquery:
1.addClass()
addClass()
Method adds the specified class name (one or more) to the matching element
Note: For elements, the class attribute can have multiple values. This method does not remove existing values, but It is to add one or more class attributes on the original basis.
2.removeClass();
Remove style
3.hasClass()
Contains a certain style
4.$(“#xxx”).attr(“class”,”className”)
Only demonstrated here isclass,attr()
The method can set other attributes, such as name, value, etc.
Note: Useattr()
The method to set the class attribute is an overwriting process; if this attribute already exists, it will be overwritten and addClass() is an appending process.
5.$("#xxx" ).removeAttr("class","className")
//移除置灰状态 $("#xxxx").removeAttr("class","disabled"); $("#xxxx").removeAttr("class","className");
If there is only one style, you can also remove the class attribute directly, but you will not get the class attribute
$("#xxxx").removeAttr("class");
Related free learning recommendations:JavaScript(video)
The above is the detailed content of How to change the class attribute in jquery. For more information, please follow other related articles on the PHP Chinese website!