如何在jQuery中将现有元素与另一个元素包装在一起?
要将现有元素与jQuery中的另一个元素包装在一起,请使用wrapAll()方法。该wrapAll()方法将匹配集中的所有元素包装到单个包装器元素中。
示例
您可以尝试运行以下代码,以了解如何在jQuery中将现有元素与另一个元素包装在一起:
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#button3").click(function(){
$('#demo, #demo2').wrapAll('<div class="demo3">');
});
});
</script>
<style>
div {
background-color: green;
}
</style>
</head>
<body>
<div id="demo">
<h2>Heading 2</h2>
<p>This is demo text.</p>
<p>Test</p>
</div>
<div id="demo2">
<h2>Heading 2</h2>
<p>This is demo text.</p>
<p>Test</p>
</div>
<button id="button3">Wrap all</button>
</body>
</html>热门推荐
10 医院探望朋友祝福语简短
11 毕业祝福语简短女朋友
12 春节虎年爸妈祝福语简短
13 儿童毕业祝福语 简短6
14 开工仪式横幅祝福语简短
15 孩子日常祝福语大全简短
16 清晨寄语诗句祝福语简短
17 结婚送亲认亲祝福语简短
18 虎年喝酒拜年祝福语简短