jQuery 操作 CSS
jQuery 拥有若干进行 CSS 操作的方法。我们将学习下面这些:
addClass() - 向被选元素添加一个或多个类
removeClass() - 从被选元素删除一个或多个类
toggleClass() - 对被选元素进行添加/删除类的切换操作
css() - 设置或返回样式属性
.important { font-weight:bold; font-size:xx-large; } .blue color:blue;
下面的例子展示如何向不同的元素添加 class 属性。当然,在添加类时,您也可以选取多个元素:
$("button").click(function(){ $("h1,h2,p").addClass("blue"); $("div").addClass("important"); });
image.png
例如同时添加:"important blue"样式
$("body div:first").addClass("important blue");
使用jQuery removeClass()
$("button").click(function(){$("h1,h2,p").removeClass("blue");});
$("h1,h2,p").toggleClass("blue");