jQuery .class 選擇器


jQuery 中的 .class 選擇器用於選擇所有具有特定類別的元素。

語法

語法如下 −

$(".class")

以上,引數 class 指定了元素的類別。

示例

現在讓我們看一個使用 jQuery .class 選擇器的示例 −

<!DOCTYPE html>
<html>
<head>
<style>
   .one {
      background-color: blue;
      color: white;
      font-size: 18px;
      border: 2px blue dashed;
   }
</style>
<script src="https://code.jquery.com/jquery-3.4.1.js"></script>
<script>
   $(document).ready(function(){
      $(".demo").addClass("one");
   });
</script>
</head>
<body>
<h1>Heading One</h1>
<h2>Heading Two</h2>
<p class="demo">Demo text 1...</p>
<p>Demo text 2...</p>
<p class="demo">Demo text 3...</p>
<p>Demo text 4...</p>
</body>
</html>

輸出

將生成以下輸出 −

更新於: 2019 年 11 月 11 日

119 瀏覽量

開啟你的 職業生涯

完成課程以獲得認證

開始使用
廣告
© . All rights reserved.