
- 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 wrap() 方法
jQuery 中的 wrap() 方法用於在選定元素集中的每個元素周圍包裝一個 HTML 結構。
注意:如果您想將 HTML 結構包裝在所有匹配的元素周圍作為一個整體,wrapAll() 是更合適的方法。
語法
以下是 jQuery 中 wrap() 方法的語法:
$(selector).wrap(wrappingElement,function(index))
引數
此方法接受以下引數:
- wrappingElement: 指定要圍繞每個選定元素包裝的 HTML 元素。可能的值包括:HTML 元素、DOM 元素、jQuery 物件。
- function (index): (可選)為每個選定元素執行的函式。
示例 1
使用 wrap() 方法與 HTML 元素
在下面的示例中,我們使用 wrap() 方法將每個 <p> 元素包裝在一個 <div> 元素中:
<html> <head> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $('p').wrap('<div></div>').css({"border": "2px solid green"}); }); }); </script> </head> <body> <p>Paragraph element.</p> <p>This is another Paragraph element.</p> <h3>Heading element.<h3> <button>Click me!</button> </body> </html>
當我們點選按鈕時,<div> 元素將圍繞每個 <p> 元素並以綠色實線邊框突出顯示。
示例 2
使用 wrap() 方法與 DOM 元素
這與第一個示例類似,它將每個 <p> 元素包裝在一個 <div> 元素中:
<html> <head> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $('p').wrap(document.createElement('div')).css({"border": "2px solid green"}); }); }); </script> </head> <body> <p>Paragraph element.</p> <p>This is another Paragraph element.</p> <h3>Heading element.<h3> <button>Click me!</button> </body> </html>
點選按鈕後,<div> 元素將圍繞每個 <p> 元素並以綠色實線邊框突出顯示。
示例 3
使用 wrap() 方法與 jQuery 物件
在這裡,我們用 <div> 元素包裝每個 <p> 元素:
<html> <head> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $('p').wrap($('<div></div>')).css({"border": "2px solid green"}); }); }); </script> </head> <body> <p>Paragraph element.</p> <p>This is another Paragraph element.</p> <h3>Heading element.<h3> <button>Click me!</button> </body> </html>
點選按鈕後,<div> 元素將圍繞每個 <p> 元素並以綠色實線邊框突出顯示。
示例 4
使用 wrap() 方法與 回撥 函式
<html> <head> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $('p').wrap(function(index){ return '<div></div>'; }).css({"border": "2px solid green"}); }); }); </script> </head> <body> <p>This is a Paragraph element.</p> <p>This is another Paragraph element.</p> <h3>Heading element.<h3> <button>Click me!</button> </body> </html>
因此,當點選按鈕時,每個 <p> 元素都用 <div> 元素包裝,並且包裝的元素將以綠色邊框突出顯示。
jquery_ref_html.htm
廣告