通过 jQuery,可以很容易地对 CSS 元素进行操作。
jQuery 操作 CSS
jQuery 拥有若干进行 CSS 操作的方法。我们将学习下面这些:
- addClass() - 向被选元素添加一个或多个类
- removeClass() - 从被选元素删除一个或多个类
- toggleClass() - 对被选元素进行添加/删除类的切换操作
- css() - 设置或返回样式属性
jQuery addClass() 方法
下面的例子展示如何向不同的元素添加 class 属性。当然,在添加类时,您也可以选取多个元素:
实例
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("h1,h2,p").addClass("red");
$("div").addClass("important");
})
})
</script>
<style>
.impotrant{
font-weight:bold;
font-size:xx-large;
}
.red{
color:#F00;}
</style>
</head>
<body>
<h1>标题 1</h1>
<h2>标题 2</h2>
<p>第一个段落。</p>
<p>第二个段落。</p>
<div>这是非常重要的文本!</div>
<br>
<button>向元素添加类</button>
</body>
</html>
效果图:
也可以在 addClass() 方法中规定多个类:
实例
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="jquery.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;
}
</head>
<body>
<div id="div1">这是一些文本。</div>
<div id="div2">这是一些文本。</div>
<br>
<button>向第一个 div 元素添加类</button>
</style>
</body>
</html>
jQuery removeClass() 方法
下面的例子演示如何不同的元素中删除指定的 class 属性:
实例
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("h1,h2,p").removeClass("red");
})
})
</script>
<style>
.important{
font-weight:bold;
font-size:xx-large;
}
.red{
color:#F00;}
</style>
</head><body><h1 class="red">标题 1</h1><h2 class="red">标题 2</h2><p class="red">第一个段落。</p><p>第二个段落。</p><div>这是非常重要的文本!</div><br><button>删除类元素</button></body></html>
效果图:
jQuery toggleClass() 方法
下面的例子将展示如何使用 jQuery toggleClass() 方法。该方法对被选元素进行添加/删除类的切换操作:
实例
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("h1,h2,p").toggleClass("red");
$("div").toggleClass("important");
})
})
</script>
<style>
.important{
font-weight:bold;
font-size:xx-large;
}
.red{
color:#F00;}
</style>
</head><body><h1 class="red">标题 1</h1><h2 class="red">标题 2</h2><p class="red">第一个段落。</p><p>第二个段落。</p><div>这是非常重要的文本!</div><br><button>切换类元素</button></body></html>
效果图:
总结:这三个元素使用大同小异。熟练其中一个,另外两个就可以不用。希望别混淆了