replaceWith()方法将选定的元素替换为新内容。
replaceWith()方法类似于replaceAll(),但是content和selector相反。
替换内容:
$(selector).replaceWith(content)
使用功能替换内容:
$(selector).replaceWith(function(index))
将所有段落替换为<h1>元素:
$("button").click(function(){
$("p").replaceWith("<h1>新标题</h1>");
});
测试看看‹/›使用功能替换内容:
$("button").click(function(){
$("p").replaceWith(function(i){
return "<h2>这个元素有索引 " + i + ".</h2>";
});
});
测试看看‹/›参数 | 描述 |
---|---|
content | 指定要插入的内容(可以包含HTML标记) 可能的值:
|
function(index) | 指定一个函数,该函数返回要替换的HTML内容
|