Home > Article > Web Front-end > How to remove class attribute in jquery
Method: 1. Use attr() to set the value of the class attribute to empty, using the syntax "$(selector).attr("class","")"; 2. Use removeAttr() to remove the class Attribute, syntax "$(selector).removeAttr("class")".
The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.
There are two ways to remove the class attribute in jquery:
Use attr() to change the value of the class attribute in the element Set to empty
Syntax:$(selector).attr("class","")
Use removeAttr() to remove elements The class attribute in
Syntax: $(selector).removeAttr("class")
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="js/jquery-1.10.2.min.js"></script> <script> $(document).ready(function() { $("button").click(function() { $(".box1").attr("class", ""); $(".box2").removeAttr("class"); }); }); </script> <style> div { border: 1px solid red; margin: 10px; } .box1 { background-color: #FFC0CB; } .box2 { background-color: green; color: white; } </style> </head> <body> <div class="box1">测试文本</div> <div class="box2">测试文本</div> <br> <button>去掉class属性</button> </body> </html>Recommended related video tutorials:
The above is the detailed content of How to remove class attribute in jquery. For more information, please follow other related articles on the PHP Chinese website!