目录

wrap( html )

描述 (Description)

wrap( html )方法使用指定的HTML内容包装每个匹配的元素。

此包装过程对于将其他结构注入文档非常有用,而不会破坏文档的原始语义质量。

语法 (Syntax)

以下是使用此方法的简单语法 -

<i>selector</i>.wrap( html )

参数 (Parameters)

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

  • elem - 将在动态创建并围绕每个目标的HTML字符串。

例子 (Example)

以下是一个简单的示例,简单地显示了此方法的用法。 当点击任何方块时,这会将目标方块包含方块 -

<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>';
               $("#destination").wrap( 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>

这将产生以下结果 -

新页面打开
↑回到顶部↑
WIKI教程 @2018