jQuery中wrapAll()方法用法实例

网络编程 2021-07-04 21:47www.168986.cn编程入门
这篇文章主要介绍了jQuery中wrapAll()方法用法,实例分析了wrapAll()方法的功能、定义及匹配元素的使用技巧,需要的朋友可以参考下

本文实例讲述了jQuery中wrapAll()方法用法。分享给大家供大家参考。具体分析如下:

此方法将所有匹配的元素用单个元素包裹起来。
此方法与wrap()方法虽然功能类似,但是有着很大区别。wrap()方法会把每一个匹配的元素都匹配一次。

语法一:

用指定的DOM元素去包裹匹配元素。

代码如下:
$(selector).wrapAll(elem)

参数列表:
参数 描述
elem 用于包裹目标的DOM元素。

实例:

代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="author" content="https://www.jb51.net/" />
<head>
<title>狼蚁SEO</title>
<style type="text/css">
    div
   {
       width:200px;
       height:200px;
       border:1px solid blue;
       margin-top:10px;
    }
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $("button").click(function(){
            $("p").wrapAll(document.getElementById("content"));
        });
    });
</script>
</head>
<body>
    <p>我是个段落</p>
    <p>我是个段落</p>
    <div id="content"></div>
    <button>用div包裹每个段落</button>
</body>
</html>

语法二:

用html标记代码包裹匹配的元素。

代码如下:
$(selector).wrapAll(html)

参数列表:
参数 描述
elem 用于包裹目标的DOM元素。

实例:

代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="author" content="https://www.jb51.net/" />
<head>
<title>狼蚁SEO</title>
<style type="text/css">
    .content
   {
        width:200px;
        height:200px;
        border:1px solid blue;
    }
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $("button").click(function(){
            $("p").wrapAll("<div class='content'></div>");
        });
    });
</script>
</head>
<body>
   <p>这是个段落</p>
   <p>这是个段落</p>
   <button>用div包裹每个段落</button>
</body>
</html>

希望本文所述对大家的jQuery程序设计有所帮助。

Copyright © 2016-2025 www.168986.cn 狼蚁网络 版权所有 Power by

长沙网络推广|微博营销|长沙seo优化|视频营销|长沙网络营销|微信营销|长沙网站建设|口碑营销|软文营销