jquery学习之路之元素类的切换toggle

对元素类的控制(增删改切换)达到不同的页面效果
<head>
<script src="/jquery/jquery-1.11.1.min.js">
</script>
<script>
$(document).ready(function(){
$("#blue").click(function(){
$("h1,h2,p").toggleClass("blue");
});
$("#red").click(function(){
$("h1,h2,p").toggleClass("red");
});
$("h1").click(function(){
$(this).addClass("green");
});
$("h2").click(function(){
$(this).removeClass("red");
});
});
</script>
<style type="text/css">
.blue
{
color:blue;
}
.red
{
color:red;
}
.green
{
color:green;
}
</style>
</head>
<body>

<h1>标题 1</h1>
<h2>标题 2</h2>
<p>这是一个段落。</p>
<p>这是另一个段落。</p>
<button id="blue">切换 CSS 类蓝色</button>
<button id="red">切换 CSS 类红色</button>
</body>

原文地址:https://www.cnblogs.com/shunzdd/p/5586016.html