jQuery CSS 类


jQuery - Get and set CSS classes


With jQuery, you can easily operate on CSS elements.


jQuery Manipulates CSS

##jQuery Owned Several ways to perform CSS manipulations. We will learn the following:

  • addClass() - Adds one or more classes to the selected element

  • removeClass() - Removes the class from the selected element Select an element to delete one or more classes

  • toggleClass() - Perform a switching operation of adding/deleting classes to the selected element

  • css( ) - Sets or returns style properties


##Instance stylesheet
The following style sheet will be used for all examples on this page:

.important
{
font-weight:bold;
font- size:xx-large;
}

.blue
{
color:blue;
}


jQuery addClass() method
The following example shows how to add the class attribute to different elements. Of course, when adding a class, you can also select multiple elements:

Instance

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://libs.baidu.com/jquery/1.10.2/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>标题 1</h1>
<h2>标题 2</h2>
<p>这是一个段落。</p>
<p>这是另外一个段落。</p>
<div>这是一些重要的文本!</div>
<br>
<button>为元素添加 class</button>

</body>
</html>

Run Instance»
Click the "Run Instance" button to view the online instance

You can also specify multiple classes in the addClass() method:

Instance

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.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;
}
</style>
</head>
<body>

<div id="div1">这是一些文本。</div>
<div id="div2">这是一些文本。</div>
<br>
<button>为第一个 div 元素添加类</button>

</body>
</html>

Run Instance»
Click the "Run Instance" button to view the online instance


##jQuery removeClass() method

#The following example demonstrates how to remove the specified class attribute in different elements:

Instance

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://libs.baidu.com/jquery/1.10.2/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">标题 1</h1>
<h2 class="blue">标题 2</h2>
<p class="blue">这是一个段落。</p>
<p>这是另外一个段落。</p>
<br>
<button>从元素中移除 class</button>
</body>
</html>

Run instance»

Click the "Run instance" button to view the online instance


jQuery toggleClass() method

The following example will show how to use the jQuery toggleClass() method. This method performs the switching operation of adding/deleting classes to the selected element:

Example

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.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 class="blue">标题 1</h1>
<h2 class="blue">标题 2</h2>
<p class="blue">这是一个段落。</p>
<p>这是另外一个段落。</p>
<br>
<button>切换 class</button>
</body>
</html>


Run Instance»

Click the "Run Instance" button to view the online instance


##jQuery css() method

We will explain the jQuery css() method in the next chapter.