- 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 wrapAll() 方法
jQuery 中的wrapAll() 方法用於在一個選定的 HTML 結構中包裹所有被選擇器匹配的元素。它將指定的 HTML 結構作為一個單元包裹在所有匹配元素的周圍。
注意:如果你想在選定元素集中的每個元素周圍包裹一個 HTML 結構,則應使用wrap() 方法。
語法
以下是 jQuery 中 wrapAll() 方法的語法:
$(selector).wrapAll(wrappingElement)
引數
以下是 jQuery 中 wrapAll() 方法的語法:
- wrappingElement: 指定要圍繞每個選定元素包裹的 HTML 元素。可能的值包括:HTML 元素、DOM 元素、jQuery 物件。
示例 1
使用 wrap() 方法和HTML 元素
在下面的例子中,我們使用 wrapAll() 方法在一個單一的 HTML 結構 (<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").wrapAll("<div></div>");
});
});
</script>
<style>
div{
border: 2px solid green;
}
</style>
</head>
<body>
<p>Paragraph element.</p>
<p>This is another Paragraph element.</p>
<button>Click me!</button>
</body>
</html>
點選按鈕後,<div> 元素將作為一個單元包裹所有 <p> 元素,並用綠色實線邊框突出顯示。
示例 2
使用 wrap() 方法和DOM 元素
這與第一個例子類似,它使用 <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').wrapAll(document.createElement('div'));
});
});
</script>
<style>
div{
border: 2px solid green;
}
</style>
</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').wrapAll($('<div></div>'));
});
});
</script>
<style>
div{
border: 2px solid green;
}
</style>
</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> 元素,並用綠色實線邊框突出顯示。
jquery_ref_html.htm
廣告
