With jQuery, it is easy to manipulate the style of elements.
jQuery has several methods for CSS manipulation. We will look at the following methods:
addClass()
- Adds one or more classes to the selected elementsremoveClass()
- Removes one or more classes from the selected elementstoggleClass()
- Toggles between adding/removing classes from the selected elementscss()
- Sets or returns the style attributeThe following stylesheet will be used for all the examples on this page:
.important {
font-weight: bold;
font-size: xx-large;
}
.blue {
color: blue;
}
The following example shows how to add class attributes to different elements. Of course you can select multiple elements, when adding classes:
$("button").click(function(){
$("h1, h2, p").addClass("blue");
$("div").addClass("important");
});
Try it Yourself »
You can also specify multiple classes within the addClass()
method:
The following example shows how to remove a specific class attribute from different elements:
The following example will show how to use the jQuery toggleClass()
method. This method toggles between adding/removing classes from the selected elements:
The jQuery css()
method will be explained in the next chapter.
For a complete overview of all jQuery CSS methods, please go to our jQuery HTML/CSS Reference.
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!