Latest web development tutorials

jQuery replaceWith () method

jQuery HTML / CSS Methods jQuery HTML / CSS Methods

Examples

The first <p> element with the new text:

$("button").click(function(){
$("p:first").replaceWith("Hello world!");
});

try it"

Definition and Usage

replaceWith () method of the selected element is replaced with new content.


grammar

$(selector).replaceWith(content,function(index))

参数 描述
content 必需。规定要插入的内容(可包含 HTML 标签)。

可能的值:

  • HTML 元素
  • jQuery 对象
  • DOM 元素
function(index) 可选。规定返回替换内容的函数。
  • index- 返回集合中元素的 index 位置。


Examples

More examples

Use function replaces the contents
How to use the function replaces the selected element is replaced with new content.


jQuery HTML / CSS Methods jQuery HTML / CSS Methods