replaceWith()方法將選定的元素替換為新內(nèi)容。
replaceWith()方法類似于replaceAll(),但是content和selector相反。
替換內(nèi)容:
$(selector).replaceWith(content)
使用功能替換內(nèi)容:
$(selector).replaceWith(function(index))
將所有段落替換為<h1>元素:
$("button").click(function(){
$("p").replaceWith("<h1>新標題</h1>");
});測試看看?/?使用功能替換內(nèi)容:
$("button").click(function(){
$("p").replaceWith(function(i){
return "<h2>這個元素有索引 " + i + ".</h2>";
});
});測試看看?/?| 參數(shù) | 描述 |
|---|---|
| content | 指定要插入的內(nèi)容(可以包含HTML標記) 可能的值:
|
| function(index) | 指定一個函數(shù),該函數(shù)返回要替換的HTML內(nèi)容
|