jquery replaceWith() method


  Translation results:

replace

UK[rɪˈpleɪs] US[rɪˈples]

vt. Replace; replace; put... back in place; (use...) replace

with

英[wɪð] 美[wɪθ]

prep.With; and, with; about; consistent with...

jquery replaceWith() methodsyntax

Function: replaceWith() method replaces the selected element with the specified HTML content or element. replaceWith() has the same effect as replaceAll(). The differences are in the syntax: the content and the position of the selector, and the replaceAll() function cannot be used for replacement.

Syntax: $(selector).replaceWith(content)

##Parameters:

ParameterDescriptioncontent Required. Specifies the content to replace the selected element. Possible values: HTML code - e.g. ("<div></div>") New element - e.g. (document.createElement("div")) Existing element - e.g. ($(".div1")) Existing elements will not be moved, only copied and wrapped around the selected element. selector Required. Specifies the element to be replaced.​

Use function to replace elements: Use function to replace selected elements with new content.

Syntax: $(selector).replaceWith(function())

Parameters:

ParametersDescriptionfunction()Required. A function that returns the new content to replace the selected element. ##

jquery replaceWith() methodexample

<html>
<head>
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
    $("p").replaceWith("<b>Hello world!</b>");
  });
});
</script>
</head>
<body>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button class="btn1">用粗体文本替换所有段落</button>
</body>
</html>
Run instance »

Click the "Run instance" button to view the online instance