Наши волонтёры ещё не перевели данную статью на Русский. Присоединяйтесь к нам и помогите сделать эту работу!
Вы можете также прочитать эту статью на English (US).

This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

The ChildNode.replaceWith() method replaces this ChildNode in the children list of its parent with a set of Node or DOMString objects. DOMString objects are inserted as equivalent Text nodes.


[Throws, Unscopable] 
void ChildNode.replaceWith((Node or DOMString)... nodes);


A set of Node or DOMString objects to replace.



Using replaceWith()

var parent = document.createElement("div");
var child = document.createElement("p");
var span = document.createElement("span");


// "<div><span></span></div>"

ChildNode.replaceWith() is unscopable

The replaceWith() method is not scoped into the with statement. See Symbol.unscopables for more information.

with(node) { 
// ReferenceError: replaceWith is not defined 


You can polyfill the replaceWith() method in Internet Explorer 10+ and higher with the following code:

function ReplaceWithPolyfill() {
  'use-strict'; // For safari, and IE > 10
  var parent = this.parentNode, i = arguments.length, currentNode;
  if (!parent) return;
  if (!i) // if there are no arguments
  while (i--) { // i-- decrements i and returns the value of i before the decrement
    currentNode = arguments[i];
    if (typeof currentNode !== 'object'){
      currentNode = this.ownerDocument.createTextNode(currentNode);
    } else if (currentNode.parentNode){
    // the value of "i" below is after the decrement
    if (!i) // if currentNode is the first argument (currentNode === arguments[0])
      parent.replaceChild(currentNode, this);
    else // if currentNode isn't the first
      parent.insertBefore(this.previousSibling, currentNode);
if (!Element.prototype.replaceWith)
    Element.prototype.replaceWith = ReplaceWithPolyfill;
if (!CharacterData.prototype.replaceWith)
    CharacterData.prototype.replaceWith = ReplaceWithPolyfill;
if (!DocumentType.prototype.replaceWith) 
    DocumentType.prototype.replaceWith = ReplaceWithPolyfill;


Specification Status Comment
The definition of 'ChildNode.replacewith()' in that specification.
Living Standard Initial definition.

Browser compatibility

Update compatibility data on GitHub
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung Internet
Basic support
Chrome Full support 54Edge Full support 17Firefox Full support 49IE No support NoOpera Full support 39Safari No support NoWebView Android Full support 54Chrome Android Full support 54Edge Mobile No support NoFirefox Android Full support 49Opera Android Full support 39Safari iOS No support NoSamsung Internet Android Full support 6.0


Full support  
Full support
No support  
No support
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.

See also

Метки документа и участники

Внесли вклад в эту страницу: Sephr, Peppesterest, zhuangyin, fscholz, subhaze, smukkekim, ght, ericyd, anonyco, stevenwdv, jszhou, jpmedley
Обновлялась последний раз: Sephr,