.replaceWith()


.replaceWith( newContent )傳回: jQuery

說明: 將匹配元素集中每個元素替換為提供的 newContent,並傳回已移除的元素集。

.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.jquery.com/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.jquery.com/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.jquery.com/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.jquery.com/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>

示範