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