replaceWith()

用提供的内容替换集合中所有匹配的元素并且返回被删除元素的集合。

.replaceWith ( newContent )

newContent 类型: String, Element, jQuery 。用来插入的内容,可能是HTML字符串,DOM元素,或者jQuery对象。

.replaceWith ( function )

function 类型:Function() 。一个函数,返回的内容会替换匹配的元素集合。

.replaceWith() 可以从DOM中移除内容,然后在这个地方插入新的内容。请看下面的例子:

<div class="container">
  <div class="inner first">Hello</div>
  <div class="inner second">And</div>
  <div class="inner third">Goodbye</div>
</div>

我们可以用指定的HTML替换第二个 inner <div>

$('div.second').replaceWith('<h2>New heading</h2>');

结果如下:

<div class="container">
  <div class="inner first">Hello</div>
  <h2>New heading</h2>
  <div class="inner third">Goodbye</div>
</div>

同样我们也可以一次性替换所有inner <div> :

$('div.inner').replaceWith('<h2>New heading</h2>');

结果如下:

<div class="container">
  <h2>New heading</h2>
  <h2>New heading</h2>
  <h2>New heading</h2>
</div>

或者我们可以选择一个元素把它当做替换的内容:

$('div.third').replaceWith($('.first'));

结果如下:

<div class="container">
  <div class="inner second">And</div>
  <div class="inner first">Hello</div>
</div>

从这个例子可以看出,用来替换的元素从老地方移到新位置,而不是复制。

.replaceWith() 方法,和大部分其他jQuery方法一样,返回jQuery对象,所以可以和其他方法链接使用,但是需要注意的是:对于该方法而言,该对象指向已经从 DOM 中被移除的对象,而不是指向替换用的对象。

从jQuery 1.4开始 .replaceWith() 都对分离的DOM元素有效。请看下面的例子, .replaceWith() 返回一个jQuery集合子包含一个段落:

$("<div/>").replaceWith("<p></p>");

.replaceWith() 方法也可以接受一个函数作为它的参数:

$('div.container').replaceWith(function() {
  return $(this).contents();
});

这个结果 <div class="container"> 被它的三个子元素 <div> 替换。函数的返回值可能是一个HTML字符串,DOM元素,或jQuery对象。

例子

点击按钮,用包含同样文字的div替换按钮:

<!DOCTYPE html>
<html>
<head>
  <style>
  button { display:block; margin:3px; color:red; width:200px; }
  div { color:red; border:2px solid blue; width:200px;
      margin:3px; text-align:center; }
  </style>
  <script src="./static/js/jquery-3.5.0.js"></script>
</head>
<body>
 
<button>First</button>
<button>Second</button>
<button>Third</button>
 
<script>
$("button").click(function () {
  $(this).replaceWith( "<div>" + $(this).text() + "</div>" );
});
</script>
 
</body>
</html>

用粗体字替换所有段落。

<!DOCTYPE html>
<html>
<head>
  <script src="./static/js/jquery-3.5.0.js"></script>
</head>
<body>
 
<p>Hello</p>
<p>cruel</p>
<p>World</p>
 
<script>
$("p").replaceWith( "<b>Paragraph. </b>" );
</script>
 
</body>
</html>

On click, replace each paragraph with a div that is already in the DOM and selected with the $() function. Notice it doesn't clone the object but rather moves it to replace the paragraph.

<!DOCTYPE html>
<html>
<head>
  <style>
  div { border:2px solid blue; color:red; margin:3px; }
  p { border:2px solid red; color:blue; margin:3px; cursor:pointer; }
  </style>
  <script src="./static/js/jquery-3.5.0.js"></script>
</head>
<body>
 
  <p>Hello</p>
  <p>cruel</p>
  <p>World</p>
 
  <div>Replaced!</div>
 
<script>
$("p").click(function () {
  $(this).replaceWith( $("div") );
});
</script>
 
</body>
</html>

On button click, replace the containing div with its child divs and append the class name of the selected element to the paragraph.

<!DOCTYPE html>
<html>
<head>
  <style>
  .container { background-color: #991; }
  .inner { color: #911; }
  </style>
  <script src="./static/js/jquery-3.5.0.js"></script>
</head>
<body>
 
<p>
  <button>Replace!</button>
</p>
<div class="container">
  <div class="inner">Scooby</div>
  <div class="inner">Dooby</div>
  <div class="inner">Doo</div>
</div>
 
<script>
$('button').bind("click", function() {
  var $container = $("div.container").replaceWith(function() {
    return $(this).contents();
  });
 
  $("p").append( $container.attr("class") );
});
</script>
 
</body>
</html>

上篇: replaceAll()