无涯教程-jQuery - Wrapall-html方法函数

58 阅读1分钟

wrapAll(Html)方法将匹配集合中的所有元素包装到单个包装元素中。

Wrapall-html - 语法

selector.wrapAll( html )

下面是此方法使用的所有参数的说明

  • html    -  动态创建并环绕每个目标的HTML字符串。

Wrapall-html - 示例

下面是一个简单的示例,一个简单的示例说明了该方法的用法。这将用一个新的正方形-包装所有的正方形

<html>
   <head>
      <title>The jQuery Example</title>
      <script type="text/javascript" 
         src="https://ajax.googleapis.com/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:10px;padding:12px; border:2px solid #666; width:60px;}
      </style>
   </head>
	
   <body>
      <p>Click on any square below to see the result:</p>
		
      <div class="div" id="destination">THIS IS TEST</div>
      <div class="div" style="background-color:blue;">ONE</div>
      <div class="div" style="background-color:green;">TWO</div>
      <div class="div" style="background-color:red;">THREE</div>
   </body>
</html>

这将产生以下输出-

参考链接

www.learnfk.com/jquery/dom-…