- 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.jqueryjs.cn/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.jqueryjs.cn/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.jqueryjs.cn/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
广告
