加载中...

.class


概述    class selector

返回值:Array<Element(s)>

描述:选择给定样式类名的所有元素。

  • V : 1.0jQuery( ".class" )

    class: 一个用来查找的类名。一个元素可以有多个类;其中只有一个必须匹配。

对于类选择器,如果浏览器支持,jQuery使用JavaScript的原生getElementsByClassName()函数来实现。

示例

实例

查找所有类是 "myClass" 的元素.

HTML 代码:
  1. <div class="notMe">div class="notMe"</div>
  2. <div class="myClass">div class="myClass"</div>
  3. <span class="myClass">span class="myClass"</span>
jQuery 代码:
  1. $(".myClass");
结果:
  1. [ <div class="myClass">div class="myClass"</div>, <span class="myClass">span class="myClass"</span> ]

实例

找到该元素的类名为“myclass”。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>class demo</title>
  6. <style>
  7. div, span {
  8. width: 120px;
  9. height: 40px;
  10. float: left;
  11. padding: 10px;
  12. margin: 10px;
  13. background-color: #EEEEEE;
  14. }
  15. </style>
  16. <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
  17. </head>
  18. <body>
  19. <div class="notMe">div class="notMe"</div>
  20. <div class="myClass">div class="myClass"</div>
  21. <span class="myClass">span class="myClass"</span>
  22. <script>
  23. $( ".myClass" ).css( "border", "3px solid red" );
  24. </script>
  25. </body>
  26. </html>

运行一下

实例

查找的元素都“的myclass”和“otherclass的的”类。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>class demo</title>
  6. <style>
  7. div, span {
  8. width: 120px;
  9. height: 40px;
  10. float: left;
  11. padding: 10px;
  12. margin: 10px;
  13. background-color: #EEEEEE;
  14. }
  15. </style>
  16. <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
  17. </head>
  18. <body>
  19. <div class="myclass">div class="notMe"</div>
  20. <div class="myclass otherclass">div class="myClass"</div>
  21. <span class="myclass otherclass">span class="myClass"</span>
  22. <script>
  23. $( ".myclass.otherclass" ).css( "border", "13px solid red" );
  24. </script>
  25. </body>
  26. </html>

运行一下


还没有评论.