replace

英 [r??ple?s]? ?美 [r??ples]??

vt.代替;替換;把…放回原位;(用…)替換

with

英 [w?e]? ?美 [w?θ]??

prep.隨著;和,跟;關(guān)于;和…一致

jquery replaceWith()方法 語法

作用:replaceWith() 方法用指定的 HTML 內(nèi)容或元素替換被選元素。replaceWith() 與?replaceAll()?作用相同。差異在于語法:內(nèi)容和選擇器的位置,以及 replaceAll() 無法使用函數(shù)進行替換。

語法:$(selector).replaceWith(content)

參數(shù):

參數(shù)描述
content? ??必需。規(guī)定替換被選元素的內(nèi)容。可能的值:HTML 代碼 - 比如 ("<div></div>")新元素 - 比如 (document.createElement("div"))已存在的元素 - 比如 ($(".div1"))已存在的元素不會被移動,只會被復制,并包裹被選元素。
selector? ??必需。規(guī)定要替換的元素。? ??

使用函數(shù)替換元素:使用函數(shù)把被選元素替換為新內(nèi)容。

語法:$(selector).replaceWith(function())

參數(shù):

參數(shù)描述
function()必需。返回待替換被選元素的新內(nèi)容的函數(shù)。?

jquery replaceWith()方法 示例

<html>
<head>
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
    $("p").replaceWith("<b>Hello world!</b>");
  });
});
</script>
</head>
<body>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button class="btn1">用粗體文本替換所有段落</button>
</body>
</html>
運行實例 ?

點擊 "運行實例" 按鈕查看在線實例