当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


JQuery .wrapAll()用法及代码示例


用法
.wrapAll( wrappingElement ) => jQuery

说明:围绕匹配元素集中的所有元素包一个 HTML 结构。

  • 添加的版本:1.2.wrapAll( wrappingElement )

    • wrappingElement
      类型:SelectorhtmlStringElementjQuery
      一个选择器、元素、HTML 字符串或 jQuery 对象,指定用于环绕匹配元素的结构。
  • 添加的版本:1.4.wrapAll( function )

    • function
      类型:Function () => StringjQuery
      一个回调函数,返回 HTML 内容或 jQuery 对象以环绕所有匹配的元素。在函数内,this指的是集合中的第一个元素。Prior to jQuery 3.0,为集合中的每个元素错误地调用回调,并接收集合中元素的索引位置作为参数。

.wrapAll() 函数可以采用可以传递给 $() 函数的任何字符串或对象来指定 DOM 结构。这个结构可以嵌套好几层,但应该只包含一个最里面的元素。该结构将围绕匹配元素集中的所有元素,作为一个组。

考虑以下 HTML:

<div class="container">
  <div class="inner">Hello</div>
  <div class="inner">Goodbye</div>
</div>

使用 .wrapAll() ,我们可以在内部 <div> 元素周围插入一个 HTML 结构,如下所示:

$( ".inner" ).wrapAll( "<div class='new' />");

新的<div> 元素是动态创建并添加到 DOM 中的。结果是一个新的 <div> 包所有匹配的元素:

<div class="container">
  <div class="new">
    <div class="inner">Hello</div>
    <div class="inner">Goodbye</div>
  </div>
</div>

例子:

围绕所有段落包一个新的 div。

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>wrapAll demo</title>
  <style>
  div {
    border: 2px solid blue;
  }
  p {
    background: yellow;
    margin: 4px;
  }
  </style>
  <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</head>
<body>
 
<p>Hello</p>
<p>cruel</p>
<p>World</p>
 
<script>
$( "p" ).wrapAll( "<div></div>" );
</script>
 
</body>
</html>

演示:

在跨度周围包一个新创建的对象树。请注意,跨度之间的任何内容都会像本例中的 <strong>(红色文本)一样被忽略。甚至跨度之间的空白也被忽略了。单击查看源代码以查看原始 html。

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>wrapAll demo</title>
  <style>
  div {
    border: 2px blue solid;
    margin: 2px;
    padding: 2px;
  }
  p {
    background: yellow;
    margin: 2px;
    padding: 2px;
  }
  strong {
    color: red;
  }
  </style>
  <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</head>
<body>
 
<span>Span Text</span>
<strong>What about me?</strong>
<span>Another One</span>
 
<script>
$( "span").wrapAll( "<div><div><p><em><b></b></em></p></div></div>" );
</script>
 
</body>
</html>

演示:

围绕所有段落包一个新的 div。

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>wrapAll demo</title>
  <style>
  div {
    border: 2px solid blue;
  }
  p {
    background: yellow;
    margin: 4px;
  }
  </style>
  <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</head>
<body>
 
<p>Hello</p>
<p>cruel</p>
<p>World</p>
 
<script>
$( "p" ).wrapAll( document.createElement( "div" ) );
</script>
 
</body>
</html>

演示:

在所有段落周围包一个 jQuery 对象双深度 div。请注意,它不会移动对象,而只是将其克隆以环绕其目标。

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>wrapAll demo</title>
  <style>
  div {
    border: 2px solid blue;
    margin: 2px;
    padding: 2px;
  }
  .doublediv {
    border-color: red;
  }
  p {
    background: yellow;
    margin: 4px;
    font-size: 14px;
  }
  </style>
  <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</head>
<body>
 
<p>Hello</p>
<p>cruel</p>
<p>World</p>
<div class="doublediv"><div></div></div>
 
<script>
$( "p" ).wrapAll( $( ".doublediv" ) );
</script>
 
</body>
</html>

演示:

相关用法


注:本文由纯净天空筛选整理自jquery.com大神的英文原创作品 .wrapAll()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。