jQuery-CSS classes and methods
jQuery-CSS classes and methods
jQuery - Get and set CSS classes
jQuery Manipulating CSS
jQuery Has several methods for performing CSS operations. We will study the following:
addClass() - Add one or more classes to the selected element
removeClass() - Remove one or more classes from the selected element
toggleClass() - Add/delete class switching operation for the selected element
css() - Set or return the style attribute
Instance style sheet
The following The style sheet will be used for all examples on this page:
.important{ font-weight:bold; font-size:xx-large; }. blue{ color:blue; }
jQuery addClass() method
The following examples show how to add the class attribute 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,h2,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>靜夜思</h1> <p>床前明月光</p> <p>疑是地上霜</p> <p>這是另外一個(gè)段落。</p> <div>這是一些重要的文本!</div> <br> <button>為元素添加 class</button> </body> </html>
You can also specify multiple classes in the addClass() method:
<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">我會(huì)變哦</div> <div id="div2">為什么我不行呢</div> <br> <button>為第一個(gè) div 元素添加類</button> </body> </html>
jQuery removeClass() Method
The following example demonstrates how to delete the specified class attribute in different 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,h2,p").removeClass("blue"); }); }); </script> <style type="text/css"> .important { font-weight:bold; font-size:xx-large; } .blue { color:blue; } </style> </head> <body> <h1>靜夜思</h1> <p>床前明月光</p> <p>疑是地上霜</p> <p>這是另外一個(gè)段落。</p> <div>這是一些重要的文本!</div> <br> <button>從元素中移除 class</button> </body> </html>
The effect of addclass is exactly the opposite.
jQuery toggleClass() method
Attribute switching function:
<!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">標(biāo)題 1</h1> <h2 class="blue">標(biāo)題 2</h2> <p class="blue">這是一個(gè)段落。</p> <p>這是另外一個(gè)段落。</p> <br> <button>切換 class</button> </body> </html>
jQuery css() method
css() method setting Or returns one or more style properties of the selected element.
Return CSS properties
To return the value of the specified CSS property, please use the following syntax:
css("propertyname");
Set CSS properties
To set the specified CSS properties, please use the following syntax:
css("propertyname","value");
Set multiple CSS properties
If you need to set multiple CSS properties, please use the following syntax:
css({"propertyname":" value","propertyname":"value",...});
<!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(){ $("p").css({"background-color":"yellow","font-size":"200%"}); }); }); </script> </head> <body> <h2>這是一個(gè)標(biāo)題</h2> <p style="background-color:#ff0000">這是一個(gè)例子</p> <p style="background-color:#00ff00">這是一個(gè)例子</p> <p style="background-color:#0000ff">這是一個(gè)例子</p> <p>這是一個(gè)例子</p> <button>為 p 元素設(shè)置多個(gè)樣式</button> </body> </html>