當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


JQuery Class Selector (“.class”)用法及代碼示例

用法
class selector

說明:選擇具有給定類的所有元素。

  • 添加的版本:1.0jQuery( ".class" )

    class: 要搜索的類。一個元素可以有多個類;隻有其中一個必須匹配。

對於類選擇器,如果瀏覽器支持,jQuery 會使用 JavaScript 的原生 getElementsByClassName() 函數。

例子:

查找具有類 "myClass" 的元素。

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>class demo</title>
  <style>
  div, span {
    width: 120px;
    height: 40px;
    float: left;
    padding: 10px;
    margin: 10px;
    background-color: #EEEEEE;
  }
  </style>
  <script src="https://code.jquery.com/jquery-3.5.0.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>

演示:

查找同時具有 "myclass" 和 "otherclass" 類的元素。

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>class demo</title>
  <style>
  div, span {
    width: 120px;
    height: 40px;
    float: left;
    padding: 10px;
    margin: 10px;
    background-color: #EEEEEE;
  }
  </style>
  <script src="https://code.jquery.com/jquery-3.5.0.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>

演示:

相關用法


注:本文由純淨天空篩選整理自jquery.com大神的英文原創作品 Class Selector (“.class”)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。