TUTORIALFrontendFETCHED
jQuery replaceWith() 方法
jQuery replaceWith() 方法 jQuery HTML/CSS 方法 实例 把第一个 <p> 元素替换为新的文本: [mycode3 type='js'] $('button').click(function(){ $('p:first').replaceWith('Hello world!')..
Updated 7/16/2026
Tutorial detail pages default to noindex.
jQuery replaceWith() 方法
实例
把第一个 <p> 元素替换为新的文本:
$("button").click(function(){
$("p:first").replaceWith("Hello world!");
});
尝试一下 »
定义和用法
replaceWith() 方法把被选元素替换为新的内容。
语法
$(selector).replaceWith(content,function(index))
| 参数 | 描述 |
|---|---|
| content | 必需。规定要插入的内容(可包含 HTML 标签)。
可能的值:
|
| function(index) | 可选。规定返回替换内容的函数。
|
更多实例
使用函数替换内容
如何使用函数替换把被选元素替换为新的内容。
jQuery HTML/CSS 方法