.replaceWith() 您所在的位置:网站首页 replacewith翻译 .replaceWith()

.replaceWith()

2024-07-01 13:58| 来源: 网络整理| 查看: 265

The .replaceWith() method removes content from the DOM and inserts new content in its place with a single call. Consider this DOM structure:

1 2 3 4 5 Hello And Goodbye

The second inner could be replaced with the specified HTML:

1 $( "div.second" ).replaceWith( "New heading" );

This results in the structure:

1 2 3 4 5 Hello New heading Goodbye

All inner elements could be targeted at once:

1 $( "div.inner" ).replaceWith( "New heading" );

This causes all of them to be replaced:

1 2 3 4 5 New heading New heading New heading

An element could also be selected as the replacement:

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

This results in the DOM structure:

1 2 3 4 And Hello

This example demonstrates that the selected element replaces the target by being moved from its old location, not by being cloned.

The .replaceWith() method, like most jQuery methods, returns the jQuery object so that other methods can be chained onto it. However, it must be noted that the original jQuery object is returned. This object refers to the element that has been removed from the DOM, not the new element that has replaced it.



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有