
- 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 addClass() 方法
jQuery 中的 addClass() 方法用於向選定的元素新增一個或多個類名。
此方法不會刪除任何現有的類屬性;它只是將一個或多個類名附加到類屬性。
注意:如果要向元素新增多個類,則需要用空格分隔類名。
語法
以下是 jQuery 中 addClass() 方法的語法:
$(selector).addClass(classname,function(index,currentclass))
引數
此方法接受以下引數:
- classname: 指定要新增到所選元素的類名。
- function(index, currentclass): 這是一個可選的回撥函式,允許您單獨操作每個選定的元素。
示例 1
在下面的示例中,我們使用 addClass() 方法向所有 <p> 元素新增類名“highlight”:
<html> <head> <style> .highlight { background-color: yellow; } </style> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").addClass("highlight"); }) }); </script> </head> <body> <p>This paragraph will be highlighted.</p> <p>This paragraph will also be highlighted.</p> <button>Click</button> </body> </html>
單擊按鈕後,類“highlight”將新增到所有段落元素。
示例 2
在此示例中,我們將多個類“highlight”和“bold”新增到選定的元素 (<p>):
<html> <head> <style> .highlight { background-color: yellow; } .bold { font-weight: bold; } </style> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").addClass("highlight bold"); }) }); </script> </head> <body> <p>This paragraph will be highlighted and bold.</p> <p>This paragraph will also be highlighted and bold.</p> <button>Click</button> </body> </html>
單擊按鈕後,類“highlight”和“bold”將新增到所有段落元素。
示例 3
在此示例中,我們使用帶回調函式的 addClass() 方法,根據每個元素的索引新增不同的類:
元素:
<html> <head> <style> .even { background-color: lightblue; } .odd { background-color: lightgreen; } </style> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").addClass(function(index) { return index % 2 === 0 ? "even" : "odd"; }); }) }); </script> </head> <body> <p>Paragraph 1</p> <p>Paragraph 2</p> <p>Paragraph 3</p> <p>Paragraph 4</p> <button>Click</button> </body> </html>
單擊按鈕後,偶數索引的 <p> 元素將以“lightblue”背景色突出顯示。奇數索引的 <p> 元素將以“lightgreen”背景色突出顯示。
jquery_ref_html.htm
廣告