1添加類css
$("button").click(function(){ $("h1,h2,p").addClass("blue"); $("div").addClass("important"); });
2刪除類html
$("button").click(function(){ $("#div1").addClass("important blue"); });
3切換操做 (添加和刪除動做切換進行)jquery
<!DOCTYPE html> <html> <head> <script src="/jquery/jquery-1.11.1.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>標題 1</h1> <h2>標題 2</h2> <p>這是一個段落。</p> <p>這是另外一個段落。</p> <button>切換 CSS 類</button> </body> </html>
4設置和返回CSSspa
4.1返回 CSS 屬性code
css("propertyname"); //返回指定CSS屬性值
4.2設置CSS屬性htm
$("p").css("background-color"); //設置單個屬性
css({"propertyname":"value","propertyname":"value",...}); //設置多個屬性值
4.3尺寸處理ip
咱們來複習一下CSS盒子模型rem
外邊距(margin)、邊框(border)、內邊距(padding)、內容(content)四個屬性io
固然能獲取尺寸大小一定能設置大小function
$("#div1").outerWidth() //獲取大小
$("button").click(function(){ //設置大小
$("#div1").width(500).height(500);
});