加载中...

replaceWith()


概述    .replaceWith( newContent )

返回值:jQuery

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

  • V : 1.2.replaceWith( newContent )

    • newContent
      类型: String, Element, jQuery
      用来插入的内容,可能是HTML字符串,DOM元素,或者jQuery对象。
  • V : 1.4.replaceWith( function )

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

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

  1. <div class="container">
  2. <div class="inner first">Hello</div>
  3. <div class="inner second">And</div>
  4. <div class="inner third">Goodbye</div>
  5. </div>

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

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

结果如下:

  1. <div class="container">
  2. <div class="inner first">Hello</div>
  3. <h2>New heading</h2>
  4. <div class="inner third">Goodbye</div>
  5. </div>

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

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

结果如下:

  1. <div class="container">
  2. <h2>New heading</h2>
  3. <h2>New heading</h2>
  4. <h2>New heading</h2>
  5. </div>

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

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

结果如下:

  1. <div class="container">
  2. <div class="inner second">And</div>
  3. <div class="inner first">Hello</div>
  4. </div>

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

.replaceWith()方法,和大部分其他jQuery方法一样,返回jQuery对象,所以可以和其他方法链接使用, 但是需要注意的是: (original)原始jQuery对象被返回。该对象指向已经从 DOM 中被移除的对象,而不是指向已经取代了它的新元素。

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

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

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

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

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

其他注意事项:

  • .replaceWith() 方法会删除与节点相关联的所有数据和事件处理程序 。
  • 在此之前的jQuery1.9,如果该集合中的第一个节点没有在文档中, .replaceWith()将尝试在当前的jQuery集合中添加或更改的节点,在这些情况下返回一个新的jQuery集合,而不是原来的集合。 该方法是否返回一个新的结果,取决于它参数的个数和是否参数节点是否在文档中。 在jQuery1.9中,.after(), .before(), 和 .replaceWith()总是返回原始未修改的集合。 尝试在节点上使用这些方法,而不影响父级元素,即,既不是该集合也不是包含他的节点被改变。

示例

实例

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

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. button { display:block; margin:3px; color:red; width:200px; }
  6. div { color:red; border:2px solid blue; width:200px;
  7. margin:3px; text-align:center; }
  8. </style>
  9. <script src="http://code.jquery.com/jquery-latest.js"></script>
  10. </head>
  11. <body>
  12. <button>First</button>
  13. <button>Second</button>
  14. <button>Third</button>
  15. <script>
  16. $("button").click(function () {
  17. $(this).replaceWith( "<div>" + $(this).text() + "</div>" );
  18. });
  19. </script>
  20. </body>
  21. </html>

运行一下

实例

用粗体字替换所有段落。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script src="http://code.jquery.com/jquery-latest.js"></script>
  5. </head>
  6. <body>
  7. <p>Hello</p>
  8. <p>cruel</p>
  9. <p>World</p>
  10. <script>
  11. $("p").replaceWith( "<b>Paragraph. </b>" );
  12. </script>
  13. </body>
  14. </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.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. div { border:2px solid blue; color:red; margin:3px; }
  6. p { border:2px solid red; color:blue; margin:3px; cursor:pointer; }
  7. </style>
  8. <script src="http://code.jquery.com/jquery-latest.js"></script>
  9. </head>
  10. <body>
  11. <p>Hello</p>
  12. <p>cruel</p>
  13. <p>World</p>
  14. <div>Replaced!</div>
  15. <script>
  16. $("p").click(function () {
  17. $(this).replaceWith( $("div") );
  18. });
  19. </script>
  20. </body>
  21. </html>

运行一下

实例

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

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .container { background-color: #991; }
  6. .inner { color: #911; }
  7. </style>
  8. <script src="http://code.jquery.com/jquery-latest.js"></script>
  9. </head>
  10. <body>
  11. <p>
  12. <button>Replace!</button>
  13. </p>
  14. <div class="container">
  15. <div class="inner">Scooby</div>
  16. <div class="inner">Dooby</div>
  17. <div class="inner">Doo</div>
  18. </div>
  19. <script>
  20. $('button').bind("click", function() {
  21. var $container = $("div.container").replaceWith(function() {
  22. return $(this).contents();
  23. });
  24. $("p").append( $container.attr("class") );
  25. });
  26. </script>
  27. </body>
  28. </html>

运行一下


还没有评论.