- 15天學(xué)會jQuery編程與實戰(zhàn)(視頻教學(xué)版) (Web前端技術(shù)叢書)
- 劉鑫
- 259字
- 2020-11-28 16:21:49
4.3 獲取或設(shè)置CSS樣式
jQuery提供了css()方法用來獲取或設(shè)置元素的一個或多個樣式屬性。這里我們需要注意可以一次設(shè)置多個。
獲取一個屬性時:
$("div").css("background-color");
設(shè)置一個屬性時:
$("div").css("background-color", "blue");
設(shè)置多個屬性時:
$("div").css({"background-color": "blue", "font-size": "200%"});
從代碼可以看出,屬性與屬性之間用逗號間隔,屬性和屬性的值之間用冒號間隔,外層用一個{}包圍。
【示例4-3】getsetcss.html
01 <script> 02 $(document).ready(function(){ 03 $("button").click(function(){ 04 $("p").css({"background-color": "yellow", "font-size": "200%"}); 05 }); 06 }); 07 </script> 08 09 <body> 10 <h2>我的故鄉(xiāng)</h2> 11 <p style="background-color:#ff0000">有條河</p> 12 <p style="background-color:#00ff00">有座山</p> 13 <p style="background-color:#0000ff">有個寺廟</p> 14 <p>它真的很美</p> 15 <button>設(shè)置P的屬性</button> 16 </body>
第04行設(shè)置了p元素的兩個屬性background-color和font-size。在瀏覽器中的默認(rèn)頁面如圖4.5所示,單擊“設(shè)置P的屬性”按鈕后,頁面效果如圖4.6所示。

圖4.5 默認(rèn)效果

圖4.6 單擊后整體修改樣式
推薦閱讀
- Puppet 4 Essentials(Second Edition)
- 劍指JVM:虛擬機實踐與性能調(diào)優(yōu)
- 算法基礎(chǔ):打開程序設(shè)計之門
- Building Mobile Applications Using Kendo UI Mobile and ASP.NET Web API
- Spring Boot實戰(zhàn)
- 軟件工程與UML案例解析(第三版)
- Spring Web Services 2 Cookbook
- 用Go語言自制編譯器
- 生成藝術(shù):Processing視覺創(chuàng)意入門
- Scratch少兒編程高手的7個好習(xí)慣
- 現(xiàn)代JavaScript編程:經(jīng)典范例與實踐技巧
- Windows 10 for Enterprise Administrators
- Spring Microservices
- Scratch超人漫游記:創(chuàng)意程序設(shè)計:STEAM創(chuàng)新教育指南
- Python程序設(shè)計案例教程