- jQuery 教程
- jQuery - 首頁
- jQuery - 路線圖
- jQuery - 概述
- jQuery - 基礎
- jQuery - 語法
- jQuery - 選擇器
- jQuery - 事件
- jQuery - 屬性
- jQuery - AJAX
- jQuery DOM 操作
- jQuery - DOM
- jQuery - 新增元素
- jQuery - 刪除元素
- jQuery - 替換元素
- jQuery CSS 操作
- jQuery - CSS 類
- jQuery - 尺寸
- jQuery - CSS 屬性
- jQuery 效果
- jQuery - 效果
- jQuery - 動畫
- jQuery - 鏈式操作
- jQuery - 回撥函式
- jQuery 遍歷
- jQuery - 遍歷
- jQuery - 遍歷祖先節點
- jQuery - 遍歷子孫節點
- jQuery UI
- jQuery - 互動
- jQuery - 小部件
- jQuery - 主題
- jQuery 參考
- jQuery - 選擇器
- jQuery - 事件
- jQuery - 效果
- jQuery - HTML/CSS
- jQuery - 遍歷
- jQuery - 其他
- jQuery - 屬性
- jQuery - 工具函式
- jQuery 外掛
- jQuery - 外掛
- jQuery - PagePiling.js
- jQuery - Flickerplate.js
- jQuery - Multiscroll.js
- jQuery - Slidebar.js
- jQuery - Rowgrid.js
- jQuery - Alertify.js
- jQuery - Progressbar.js
- jQuery - Slideshow.js
- jQuery - Drawsvg.js
- jQuery - Tagsort.js
- jQuery - LogosDistort.js
- jQuery - Filer.js
- jQuery - Whatsnearby.js
- jQuery - Checkout.js
- jQuery - Blockrain.js
- jQuery - Producttour.js
- jQuery - Megadropdown.js
- jQuery - Weather.js
- jQuery 有用資源
- jQuery - 問答
- jQuery - 快速指南
- jQuery - 有用資源
- jQuery - 討論
jQuery .class 選擇器
jQuery 中的.class 選擇器用於選擇所有具有特定 class 屬性的 HTML 元素。
HTML 中的 class 屬性用於為 HTML 元素分配一個或多個類名,允許 CSS 和 JavaScript 選擇和操作具有特定類的元素。
語法
以下是 jQuery .class 選擇器的語法:
$(".class")
引數
class 選擇器指定要選擇的元素的類。
示例 1
以下示例選擇具有類 "highlight" 的元素並將它們的背景顏色更改為黃色:
<html>
<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$(".highlight").css("background-color", "yellow");
})
});
</script>
</head>
<body>
<p class="highlight">This text will be highlighted.</p>
<button>Click</button>
</body>
</html>
單擊按鈕後,具有類 "highlight" 的選中(段落)元素將以黃色背景突出顯示。
示例 2
在此示例中,我們選擇所有具有類 "highlight" 的元素:
<html>
<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$(".highlight").css("background-color", "yellow");
})
});
</script>
</head>
<body>
<p class="highlight">Paragraph element with (class "highlight").</p>
<p>This text won't be highlighted.</p>
<p class="highlight">Paragraph element with (class "highlight").</p>
<p>This text won't be highlighted.</p>
<p class="highlight">Paragraph element with (class "highlight").</p>
<button>Click</button>
</body>
</html>
單擊按鈕後,所有具有類 "highlight" 的段落元素將以黃色背景突出顯示。
示例 3
此示例選擇具有類 "content" 的元素,並將其內容替換為新文字:
<html>
<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$(".content").html("Content modified...");
})
});
</script>
</head>
<body>
<div class="content">Hello mate, click the button below.</div>
<button>Click</button>
</body>
</html>
單擊按鈕後,具有 "content" 類的 <div> 元素中的內容將被替換為 "Content modified..."。
jquery_ref_selectors.htm
廣告
