We're looking for a user researcher to understand the needs of developers and designers. Is this you or someone you know? Check out the post: https://mzl.la/2IGzdXS

I volontari di MDN non hanno ancora tradotto questo articolo in Italiano. Unisciti a noi e traducilo tu stesso.
Puoi anche consultare l’articolo in 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 ReplaceWith(Ele) {
  'use-strict'; // For safari, and IE > 10
  var parent = this.parentNode,
      i = arguments.length,
      firstIsNode = +(parent && typeof Ele === 'object');
  if (!parent) return;
  while (i-- > firstIsNode){
    if (parent && typeof arguments[i] !== 'object'){
      arguments[i] = document.createTextNode(arguments[i]);
    } if (!parent && arguments[i].parentNode){
    parent.insertBefore(this.previousSibling, arguments[i]);
  if (firstIsNode) parent.replaceChild(Ele, this);
if (!Element.prototype.replaceWith)
    Element.prototype.replaceWith = ReplaceWith;
if (!CharacterData.prototype.replaceWith)
    CharacterData.prototype.replaceWith = ReplaceWith;
if (!DocumentType.prototype.replaceWith) 
    DocumentType.prototype.replaceWith = ReplaceWith;


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

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support54 No49 No39 No
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support5454 No4939 No6.0

See also

Tag del documento e collaboratori

Hanno collaborato alla realizzazione di questa pagina: zhuangyin, fscholz, subhaze, smukkekim, ght, ericyd, anonyco, stevenwdv, jszhou, jpmedley
Ultima modifica di: zhuangyin,