jQuery replaceWith() 方法
实例
用新文本替换第一个 <p> 元素:
<!DOCTYPE html><html><head><script src="https://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script><script>$(document).ready(function(){$("button").click(function(){$("p:first").replaceWith("Hello world!");});});</script></head><body><p>这是一个段落</p><p>这是另外一个段落</p><button>用新文本替换第一个 p 元素</button></body></html>
定义与用法
replaceWith() 方法将选定的元素替换为新内容。
语法
$(selector).replaceWith(content,function(index))
| 参数 | 描述 |
|---|---|
| content | 必填。指定要插入的内容(可以包含HTML 标记) 值可以为:
|
| function(index) | 可选。 指定返回要替换的内容的函数
|