本文实例讲述了jQuery实现获取及设置CSS样式操作。分享给大家供大家参考,具体如下:
addClass():向被选元素添加一个或多个类
removeClass():从被选元素删除一个或多个类
toggleClass():对被选元素进行添加/删除类的切换操作
css():设置或返回被选元素的一个或多个样式属性
样式表实例:
.important{
font-weight:bold;
font-size:xx-large;
}
.blue{
color:blue;
}addClass()
<!DOCTYPE html>
<html>
<head>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("h1,h2,p").addClass("blue");
$("div").addClass("important");
});
});
</script>
<style type="text/css">
.important{
font-weight:bold;
font-size:xx-large;
}
.blue{
color:blue;
}
</style>
</head>
<body>
<h1>she is gone</h1>
<h2>no say anymore</h2>
<p>but i miss you</p>
<div>can you come back</div>
</br>
<button>addClass</button>
</body>
</html>运行效果:

可以在
addClass()方法中规定多个类:
$("button").click(function(){
$("#div1").addClass("important blue");
});removeClass()
<!DOCTYPE html>
<html>
<head>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("h1,h2,p").removeClass("blue");
});
});
</script>
<style type="text/css">
.important{
font-weight:bold;
font-size:xx-large;
}
.blue{
color:blue;
}
</style>
</head>
<body>
<h1 class="blue">is she ?</h1>
<h2 class="blue">i do not know</h2>
<p class="blue">queit</p>
<br>
<button>removeClass</button>
</body>
</html>运行结果:

toggleClass()
<!DOCTYPE html>
<html>
<head>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>










