jQuery-CSS類和方法
jQuery-CSS類和方法
jQuery -?獲取并設(shè)置 CSS 類
jQuery 操作 CSS
jQuery 擁有若干進(jìn)行 CSS 操作的方法。我們將學(xué)習(xí)下面這些:
addClass() - 向被選元素添加一個(gè)或多個(gè)類
removeClass() - 從被選元素刪除一個(gè)或多個(gè)類
toggleClass() - 對(duì)被選元素進(jìn)行添加/刪除類的切換操作
css() - 設(shè)置或返回樣式屬性
實(shí)例樣式表
下面的樣式表將用于本頁(yè)的所有例子:
.important{ font-weight:bold; font-size:xx-large; }. blue{ color:blue; }
jQuery addClass() 方法
下面的例子展示如何向不同的元素添加 class 屬性。當(dāng)然,在添加類時(shí),您也可以選取多個(gè)元素:
<!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>
您也可以在 addClass() 方法中規(guī)定多個(gè)類:
<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() 方法
下面的例子演示如何在不同的元素中刪除指定的 class 屬性:
<!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>
與addclass作用剛好相反。
jQuery toggleClass() 方法
屬性切換功能:
<!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() 方法
css() 方法設(shè)置或返回被選元素的一個(gè)或多個(gè)樣式屬性。
返回 CSS 屬性
如需返回指定的 CSS 屬性的值,請(qǐng)使用如下語法:
css("propertyname");
設(shè)置 CSS 屬性
如需設(shè)置指定的 CSS 屬性,請(qǐng)使用如下語法:
css("propertyname","value");
設(shè)置多個(gè) CSS 屬性
如需設(shè)置多個(gè) CSS 屬性,請(qǐng)使用如下語法:
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>