XML DOM removeChild() method

Definition and Usage

The removeChild() method deletes a child node.

If successful, returns the deleted node, otherwise returns NULL.

Syntax:

elementNode.removeChild(node)
Parameter Description
node Required. Specifies the child node to be deleted.

Example

In all examples, we will use the XML file books.xml, and the JavaScript function loadXMLDoc().

The following code snippet deletes the last child node of the first <book> element:

//check if last child node is an element node
fonksiyon get_lastchild(n)
{
x=n.lastChild;
while (x.nodeType!=1)
  {
  x=x.previousSibling;
  }
return x;
}
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book")[0];
deleted_node=x.removeChild(get_lastchild(x));
document.write("Silinen düğüm: " + deleted_node.nodeName);

Çıktı:

Silinen düğüm: price

Açıklama:Internet Explorer, aralarındaki beyaz metin düğümlerini (örneğin, satır sonu işaretleri) göz ardı ederken, Mozilla bunu yapmaz. Bu nedenle, yukarıdaki örnekte, doğru alt elementi almak için bir fonksiyon oluşturduk.

İpucu:IE ve Mozilla tarayıcıları arasındaki farklar hakkında daha fazla bilgi için, CodeW3C.com'un XML DOM eğitim sayfasını ziyaret edin DOM tarayıcı Bu bölüm.