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);
});