jQuery CSS 类
With jQuery, CSS elements can be easily manipulated. Switch CSS classes
jQuery operations CSS
jQuery has several methods for performing CSS operations. We will learn the following:
addClass() - Adds one or more classes to the selected element
removeClass() - Removes the class from the selected element Select an element to delete one or more classes
toggleClass() - Perform a switching operation of adding/deleting classes to the selected element
css( ) - Set or return style properties
jQuery addClass() method
The following example shows how Add class attributes to different elements. Of course, when adding a class, you can also select multiple elements:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ $("h1,p").addClass("blue"); $("div").addClass("important"); }); }); </script> <style type="text/css"> .important { font-weight:bold; font-size:xx-large; } .blue { color:blue; } </style> </head> <body> <h1>标题 1</h1> <h2>标题 2</h2> <p>这是一个段落。</p> <p>这是另外一个段落。</p> <div>这是一些重要的文本!</div> <br> <button>为元素添加 class</button> </body> </html>
Run the program to try it
You can also specify multiple classes in the addClass() method:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ $("#div1").addClass("important blue"); }); }); </script> <style type="text/css"> .important { font-weight:bold; font-size:xx-large; } .blue { color:blue; } </style> </head> <body> <div id="div1">这是一些文本。</div> <div id="div2">这是一些文本。</div> <br> <button>为第一个 div 元素添加类</button> </body> </html>
Run the program to try it
jQuery removeClass() method
The following example demonstrates how Delete the specified class attribute in different elements:
Example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ $("h1,h2,p").removeClass("blue"); }); }); </script> <style type="text/css"> .important { font-weight:bold; font-size:xx-large; } .blue { color:blue; } </style> </head> <body> <h1 class="blue">标题 1</h1> <h2 class="blue">标题 2</h2> <p class="blue">这是一个段落。</p> <p>这是另外一个段落。</p> <br> <button>从元素中移除 class</button> </body> </html>
Run the program to try it
jQuery toggleClass() method
The following example will show how to use the jQuery toggleClass() method. This method performs the switching operation of adding/deleting classes to the selected elements:
Example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ $("h1,h2,p").toggleClass("blue"); }); }); </script> <style type="text/css"> .blue { color:blue; } </style> </head> <body> <h1 class="blue">标题 1</h1> <h2 >标题 2</h2> <p class="blue">这是一个段落。</p> <p>这是另外一个段落。</p> <br> <button>切换 class</button> </body> </html>
Run the program and try it