概述 .addClass( className )
返回值:jQuery
描述:为每个匹配的元素添加指定的样式类名
String
this
指向匹配元素集合中的当前元素。
值得注意的是这个方法不会替换一个样式类名。它只是简单的添加一个样式类名到元素上。
在jQuery 1.12/2.2 版本之前, .addClass()
方法操纵是选定元素的className
特性(property),不是class
属性(attribute)。一旦特性(property)改变,浏览器就会更新相应地的属性(attribute)。此行为的一个言外之意是,这种方法只适用于HTML
DOM语义的文档(例如,不是纯XML文档)。
在jQuery1.12/2.2中,改变了这种行为以改善对XML文档,包括SVG的支持。从这个版本开始,class
属性(attribute)被替换(手册网注:这个版本开始,.addClass()
方法操作的是class
属性(attribute),而不是className
特性(property))。因此,..addClass()
可以在XML或SVG文档中使用。
对所有匹配的元素可以一次添加多个用空格隔开的样式类名, 像这样:
- $("p").addClass("myClass yourClass");
这个方法通常和.removeClass()
一起使用,用来切换元素的样式, 像这样:
- $("p").removeClass("myClass noClass").addClass("yourClass");
这里, myClass
和 noClass
样式名在所有段落上被移除, 然后 yourClass
被添加。
自 jQuery 1.4开始, .addClass()
方法允许我们通过传递一个用来设置样式类名的函数。
- $("ul li:last").addClass(function(index) {
- return "item-" + index;
- });
给定一个有2个<li>
元素的无序列表,这个示例将在最后一个<li>
元素上加上"item-1"样式。
示例
为匹配的元素加上 'selected' 类
$("p").addClass("selected");
$("p").addClass("selected1 selected2");
给li加上不同的class
<ul>
<li>Hello</li>
<li>Hello</li>
<li>Hello</li>
</ul>
$('ul li:last').addClass(function() {
return 'item-' + $(this).index();
});
在匹配的元素上加上'selected'样式。
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>addClass demo</title>
- <style>
- p {
- margin: 8px;
- font-size: 16px;
- }
- .selected {
- color: blue;
- }
- .highlight {
- background: yellow;
- }
- </style>
- <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
- </head>
- <body>
- <p>Hello</p>
- <p>and</p>
- <p>Goodbye</p>
- <script>
- $( "p" ).last().addClass( "selected" );
- </script>
- </body>
- </html>
运行一下
在匹配的元素上加上'selected'和 'highlight' 样式。
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>addClass demo</title>
- <style>
- p {
- margin: 8px;
- font-size: 16px;
- }
- .selected {
- color: red;
- }
- .highlight {
- background: yellow;
- }
- </style>
- <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
- </head>
- <body>
- <p>Hello</p>
- <p>and</p>
- <p>Goodbye</p>
- <script>
- $( "p:last" ).addClass( "selected highlight" );
- </script>
- </body>
- </html>
运行一下
Pass in a function to .addClass() to add the "green" class to a div that already has a "red" class.
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>addClass demo</title>
- <style>
- div {
- background: white;
- }
- .red {
- background: red;
- }
- .red.green {
- background: green;
- }
- </style>
- <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
- </head>
- <body>
- <div>This div should be white</div>
- <div class="red">This div will be green because it now has the "green" and "red" classes.
- It would be red if the addClass function failed.</div>
- <div>This div should be white</div>
- <p>There are zero green divs</p>
- <script>
- $( "div" ).addClass(function( index, currentClass ) {
- var addedClass;
- if ( currentClass === "red" ) {
- addedClass = "green";
- $( "p" ).text( "There is one green div" );
- }
- return addedClass;
- });
- </script>
- </body>
- </html>
运行一下