replace
英[r??ple?s]? ?美[r??ples]??
vt.代替;替換;把…放回原位;(用…)替換
with
英[w?e]? ?美[w?θ]??
prep.隨著;和,跟;關(guān)於;和…一致
jQuery的replaceWith()方法 語(yǔ)法
作用:replaceWith() 方法以指定的 HTML 內(nèi)容或元素取代被選元素。 replaceWith() 與?replaceAll()?作用相同。差異在於語(yǔ)法:內(nèi)容和選擇器的位置,以及 replaceAll() 無(wú)法使用函數(shù)進(jìn)行替換。
語(yǔ)法:$(selector).replaceWith(content)
參數(shù):
參數(shù) | 描述 |
content? ?? | 必要。規(guī)定替換被選元素的內(nèi)容??赡艿闹?HTML 程式碼- 如("<div></div>")新元素- 例如("<div></div>")新元素- 例如(document.createElement("div"))已存在的元素-($(".div1"))已存在的元素不會(huì)被移動(dòng),只會(huì)被複製,並包裹被選元素。 |
selector? ?? | 必要。規(guī)定要替換的元素。? ?? |
使用函數(shù)取代元素:使用函數(shù)將被選元素替換為新內(nèi)容。
語(yǔ)法:$(selector).replaceWith(function())
參數(shù):
參數(shù) | 描述 |
function() | 必要。傳回待替換被選元素的新內(nèi)容的函數(shù)。? |
jQuery的replaceWith()方法 範(fàn)例
<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>
#點(diǎn)擊 "執(zhí)行實(shí)例" 按鈕查看線(xiàn)上實(shí)例