加载中...

addClass()


概述    .addClass( className )

返回值:jQuery

描述:为每个匹配的元素添加指定的样式类名

  • V : 1.0.addClass( className )

    • className
      类型: String
      为每个匹配元素所要增加的一个或多个样式名。
  • V : 1.4.addClass( function(index, currentClass) )

    • function(index, currentClass)
      类型: Function( Integer index, String currentClassName ) => String
      这个函数返回一个或更多用空格隔开的要增加的样式名。接收index 参数表示元素在匹配集合中的索引位置和html 参数表示元素上原来的 HTML 内容。在函数中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文档中使用。

对所有匹配的元素可以一次添加多个用空格隔开的样式类名, 像这样:

  1. $("p").addClass("myClass yourClass");

这个方法通常和.removeClass()一起使用,用来切换元素的样式, 像这样:

  1. $("p").removeClass("myClass noClass").addClass("yourClass");

这里, myClassnoClass 样式名在所有段落上被移除, 然后 yourClass 被添加。

自 jQuery 1.4开始, .addClass() 方法允许我们通过传递一个用来设置样式类名的函数。

  1. $("ul li:last").addClass(function(index) {
  2. return "item-" + index;
  3. });

给定一个有2个<li>元素的无序列表,这个示例将在最后一个<li>元素上加上"item-1"样式。

示例

参数class 实例

为匹配的元素加上 'selected' 类

jQuery 代码:
  1. $("p").addClass("selected");
  2. $("p").addClass("selected1 selected2");

回调函数 实例

给li加上不同的class

HTML 代码:
  1. <ul>
  2. <li>Hello</li>
  3. <li>Hello</li>
  4. <li>Hello</li>
  5. </ul>
jQuery 代码:
  1. $('ul li:last').addClass(function() {
  2. return 'item-' + $(this).index();
  3. });

实例

在匹配的元素上加上'selected'样式。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>addClass demo</title>
  6. <style>
  7. p {
  8. margin: 8px;
  9. font-size: 16px;
  10. }
  11. .selected {
  12. color: blue;
  13. }
  14. .highlight {
  15. background: yellow;
  16. }
  17. </style>
  18. <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
  19. </head>
  20. <body>
  21. <p>Hello</p>
  22. <p>and</p>
  23. <p>Goodbye</p>
  24. <script>
  25. $( "p" ).last().addClass( "selected" );
  26. </script>
  27. </body>
  28. </html>

运行一下

实例

在匹配的元素上加上'selected'和 'highlight' 样式。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>addClass demo</title>
  6. <style>
  7. p {
  8. margin: 8px;
  9. font-size: 16px;
  10. }
  11. .selected {
  12. color: red;
  13. }
  14. .highlight {
  15. background: yellow;
  16. }
  17. </style>
  18. <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
  19. </head>
  20. <body>
  21. <p>Hello</p>
  22. <p>and</p>
  23. <p>Goodbye</p>
  24. <script>
  25. $( "p:last" ).addClass( "selected highlight" );
  26. </script>
  27. </body>
  28. </html>

运行一下

实例

Pass in a function to .addClass() to add the "green" class to a div that already has a "red" class.

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>addClass demo</title>
  6. <style>
  7. div {
  8. background: white;
  9. }
  10. .red {
  11. background: red;
  12. }
  13. .red.green {
  14. background: green;
  15. }
  16. </style>
  17. <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
  18. </head>
  19. <body>
  20. <div>This div should be white</div>
  21. <div class="red">This div will be green because it now has the "green" and "red" classes.
  22. It would be red if the addClass function failed.</div>
  23. <div>This div should be white</div>
  24. <p>There are zero green divs</p>
  25. <script>
  26. $( "div" ).addClass(function( index, currentClass ) {
  27. var addedClass;
  28. if ( currentClass === "red" ) {
  29. addedClass = "green";
  30. $( "p" ).text( "There is one green div" );
  31. }
  32. return addedClass;
  33. });
  34. </script>
  35. </body>
  36. </html>

运行一下


还没有评论.