
- 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 add() 方法
jQuery 中的add()方法用於向匹配元素集合新增元素,從而擴充套件選擇範圍。
此方法接受兩個引數:“element”和“context”。如果我們沒有指定context引數,則add()方法會在整個文件中向選擇新增元素。如果提供了context引數,則add()方法只在指定的context元素內新增元素。
語法
以下是jQuery中jQuery add()方法的語法:
$(selector).add(element,context)
引數
此方法接受以下引數:
element: 它可以是選擇器表示式、jQuery物件、一個或多個元素或要新增到現有元素組的HTML片段。
context(可選):指定選擇器表示式應開始匹配的文件中的位置。
示例 1
下面的例子選擇標題(<h2>) 並向其中新增元素<p>和<div>,使它們的背景顏色變為黃色:
<html> <head> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(document).ready(function(){ $('h2').add('p').add('div').css('background-color', 'yellow'); }); </script> </head> <body> <h2>Welcome to Clubhouse</h2> <p>I'm Goofy.</p> <p>I'm Mickey Mouse.</p> <div>I'm Daisy Duck.</div> <span>I'm Minnie Mouse.</span> </body> </html>
當我們執行上面的程式時,它會為<h2>、<p>和<div>元素新增黃色背景顏色,但不會為<span>元素新增。
示例 2
下面的例子為第二個span元素(索引為1)新增css樣式(background-color: yellow),如同現有的p元素一樣:
<html> <head> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(document).ready(function(){ // Select all list items and add class 'highlight' to them $('p').add(document.getElementsByTagName("span")[1]).css('background-color', 'yellow'); }); </script> </head> <body> <p>I'm Goofy.</p> <p>I'm Mickey Mouse.</p> <span>I'm Minnie Mouse.</span><span>I'm Daisy Duck.</span> </body> </html>
當我們執行上面的程式時,它會為“p”和第二個“span”元素新增background-color: yellow。
jquery_ref_traversing.htm
廣告