Jquery教程
Jquery动效
Jquery HTML/CSS
Jquery 事件
jquery常用

jQuery wrapAll()

jQuery wrapAll()方法用于将指定的HTML元素包装在一组匹配元素中的所有选定元素周围。
语法:
$(selector).wrapAll(wrappingElement)

jQuery wrapAll()方法的参数

参数 说明
wrappingElement 这是强制性参数。它指定环绕所选元素的HTML元素。其可能的值为: HTML元素 jQuery对象 DOM元素

jQuery wrapAll()方法的示例

让我们以一个示例来演示jQuery wrapAll()方法。
<!DOCTYPE html>
<html>
<head>
<script src="/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $("p").wrapAll("<div></div>");
    });
});
</script>
<style>
div{background-color: pink;}
</style>
</head>
<body>
<p>Hello Guys!</p>
<p>This is lidihuo.com</p>
<button>Wrap a div element around all p elements</button>
</body>
</html>
输出:

Hello Guys!

This is lidihuo.com

jQuery wrapAll()示例2

<!DOCTYPE html>
<html>
<head>
<title>The jQuery Example</title>
<script type="text/javascript" src="/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript" language="javascript">
         $(document).ready(function() {
            $("div").click(function () {
               var content = "<div class='div'></div>";
               $("div").wrapAll( content );
            });
         });
      </script>
<style>
.div{ margin:5px;padding:2px; border:2px solid #666; width:60px;}
</style>
</head>
<body>
<p>Click on any square to wrap the squares into a new square:</p>
<div class="div" id="destination">We are going to wrap this text</div>
<div class="div" style="background-color:orange;">ONE</div>
<div class="div" style="background-color:yellow;">TWO</div>
<div class="div" style="background-color:green;">THREE</div>
</body>
</html>
输出:

Click on any square to wrap the squares into a new square:

We are going to wrap this text
ONE
TWO
THREE

jQuery wrapAll()示例3

带有打开按钮的wrapAll()示例:
<!DOCTYPE html>
<html>
<head>
<script src="/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("#btn1").click(function(){
        $("p").wrapAll("<div></div>");
    });
    $("#btn2").click(function(){
        $("p").unwrap();
    });
});
</script>
<style>
div{background-color: pink;}
</style>
</head>
<body>
<p>Hello Guys!</p>
<p>This is lidihuo.com</p>
<button id="btn1">Wrap a div element around all p element</button>
<button id="btn2">Unwrap</button>
</body>
</html>
输出:

Hello Guys!

This is lidihuo.com

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4