3 modification methods: 1. Use attr() to modify the value of the class attribute, the syntax is "element object.attr("class","new class name");". 2. Use prop() to modify the value of the class attribute, the syntax is "element object.prop("class","new class name");". 3. Use removeClass() and addClass(), the syntax is "element object.removeClass("old class name").addClass("new class name");".
The operating environment of this tutorial: windows7 system, jquery3.6.1 version, Dell G3 computer.
jquery method to change classname (class name)
Method 1: Use attr() to modify
attr() can set the attributes and values of elements
You only need to modify the value of the class attribute, syntax:
$(selector).attr("class","新类名");
Example
Method 2: Use prop() to modify
attr() can set the attributes and values of the element
You only need to modify the class attribute Just change the value and modify the syntax:
$(selector).prop("属性名","新类名");
Example
##Method 3: Modify using removeClass() and addClass()