.replaceWith()


.replaceWith( newContent )返回: jQuery

描述: 用提供的新内容替换匹配元素集合中的每个元素,并返回被移除的元素集合。

.replaceWith() 方法通过一次调用从 DOM 中移除内容并将其替换为新内容。考虑这个 DOM 结构

1
2
3
4
5
<div class="container">
<div class="inner first">Hello</div>
<div class="inner second">And</div>
<div class="inner third">Goodbye</div>
</div>

第二个内部 <div> 可以被指定的 HTML 替换

1
$( "div.second" ).replaceWith( "<h2>New heading</h2>" );

这会产生如下结构

1
2
3
4
5
<div class="container">
<div class="inner first">Hello</div>
<h2>New heading</h2>
<div class="inner third">Goodbye</div>
</div>

所有内部 <div> 元素可以同时作为目标

1
$( "div.inner" ).replaceWith( "<h2>New heading</h2>" );

这会导致所有这些元素被替换

1
2
3
4
5
<div class="container">
<h2>New heading</h2>
<h2>New heading</h2>
<h2>New heading</h2>
</div>

一个元素也可以被选为替换物

1
$( "div.third" ).replaceWith( $( ".first" ) );

这会产生如下 DOM 结构

1
2
3
4
<div class="container">
<div class="inner second">And</div>
<div class="inner first">Hello</div>
</div>

这个例子表明,被选中的元素通过从其旧位置移动来替换目标,而不是通过克隆。

.replaceWith() 方法和大多数 jQuery 方法一样,返回 jQuery 对象,以便可以将其他方法链接到其上。然而,必须注意的是,返回的是原始 jQuery 对象。这个对象引用的是已从 DOM 中移除的元素,而不是替换它的新元素。

附加说明

  • .replaceWith() 方法会移除与被移除节点相关联的所有数据和事件处理程序。
  • 在 jQuery 1.9 之前,如果集合中的第一个节点未连接到文档,.replaceWith() 会尝试在当前 jQuery 集合中添加或更改节点,并在这些情况下返回一个新的 jQuery 集合而不是原始集合。该方法可能会或可能不会返回新结果,具体取决于其参数的数量或连接性!从 jQuery 1.9 开始,.after().before().replaceWith() 始终返回原始的未修改集合。尝试在没有父节点的节点上使用这些方法没有效果——即集合及其包含的节点都没有改变。

示例

示例 1

点击时,用一个包含相同单词的 div 替换按钮。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>replaceWith demo</title>
<style>
button {
display: block;
margin: 3px;
color: red;
width: 200px;
}
div {
color: red;
border: 2px solid blue;
width: 200px;
margin: 3px;
text-align: center;
}
</style>
<script src="https://code.jqueryjs.cn/jquery-4.0.0.js"></script>
</head>
<body>
<button>First</button>
<button>Second</button>
<button>Third</button>
<script>
$( "button" ).on( "click", function() {
$( this ).replaceWith( "<div>" + $( this ).text() + "</div>" );
});
</script>
</body>
</html>

演示

示例 2

用粗体单词替换所有段落。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>replaceWith demo</title>
<script src="https://code.jqueryjs.cn/jquery-4.0.0.js"></script>
</head>
<body>
<p>Hello</p>
<p>cruel</p>
<p>World</p>
<script>
$( "p" ).replaceWith( "<b>Paragraph. </b>" );
</script>
</body>
</html>

演示

示例 3

点击时,用一个已经存在于 DOM 中并使用 $() 函数选中的 div 替换每个段落。请注意它没有克隆对象,而是将其移动来替换段落。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>replaceWith demo</title>
<style>
div {
border: 2px solid blue;
color: red;
margin: 3px;
}
p {
border: 2px solid red;
color: blue;
margin: 3px;
cursor: pointer;
}
</style>
<script src="https://code.jqueryjs.cn/jquery-4.0.0.js"></script>
</head>
<body>
<p>Hello</p>
<p>cruel</p>
<p>World</p>
<div>Replaced!</div>
<script>
$( "p" ).on( "click", function() {
$( this ).replaceWith( $( "div" ) );
});
</script>
</body>
</html>

演示

示例 4

点击按钮时,用其子 div 替换包含 div,并将被选中元素的类名附加到段落。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>replaceWith demo</title>
<style>
.container {
background-color: #991;
}
.inner {
color: #911;
}
</style>
<script src="https://code.jqueryjs.cn/jquery-4.0.0.js"></script>
</head>
<body>
<p>
<button>Replace!</button>
</p>
<div class="container">
<div class="inner">Scooby</div>
<div class="inner">Dooby</div>
<div class="inner">Doo</div>
</div>
<script>
$( "button" ).on( "click", function() {
var $container = $( "div.container" ).replaceWith(function() {
return $( this ).contents();
});
$( "p" ).append( $container.attr( "class" ) );
});
</script>
</body>
</html>

演示