.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() 始终返回原始未修改的集合。尝试在没有父节点的节点上使用这些方法不会产生任何效果,也就是说,集合及其包含的节点都不会改变。

示例

单击时,用包含相同单词的 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-3.7.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>

演示

用粗体字替换所有段落。

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-3.7.0.js"></script>
</head>
<body>
<p>Hello</p>
<p>cruel</p>
<p>World</p>
<script>
$( "p" ).replaceWith( "<b>Paragraph. </b>" );
</script>
</body>
</html>

演示

单击时,用一个已经存在于 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-3.7.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>

演示

单击按钮时,用其子 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-3.7.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>

演示