Class Selector (“.class”)

class selector

version added: 1.0jQuery('.class')

  • class
    一个用来搜索的类名。一个元素可以有多个类;其中只有一个必须匹配。

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

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

Examples:

Example: Finds the element with the class "myClass".

<!DOCTYPE html>
<html>
<head>
  <style>
  div,span {
    width: 100px;
    height: 40px;
    float:left;
    padding: 10px;
    margin: 10px;
    background-color: #EEEEEE;
  }
  </style>
  <script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
  <div class="notMe">div class="notMe"</div>

  <div class="myClass">div class="myClass"</div>
  <span class="myClass">span class="myClass"</span>
<script>$(".myClass").css("border","3px solid red");</script>

</body>
</html>

Demo:

Example: Finds the element with both "myclass" and "otherclass" classes.

<!DOCTYPE html>
<html>
<head>
  <style>
  div,span {
    width: 100px;
    height: 40px;
    float:left;
    padding: 10px;
    margin: 10px;
    background-color: #EEEEEE;
  }
  </style>
  <script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
  <div class="myclass">div class="notMe"</div>

  <div class="myclass otherclass">div class="myClass"</div>
  <span class="myclass otherclass">span class="myClass"</span>
<script>$(".myclass.otherclass").css("border","13px solid red");</script>

</body>
</html>

Demo:

设计酷,jquery1.6 api中文手册