Penggunaan fungsi REMOVECHILD pada PHP

<Elemen Object

Contoh

Lepaskan pertama <li> elemen dari daftar:

var list = document.getElementById("myList");   // Get the <ul> element with id="myList"
list.removeChild(list.childNodes[0]);           // Remove <ul>'s first child node (index 0)

Sebelum menghapus:

  • Coffee
  • Tea
  • Milk

Setelah menghapus:

  • Tea
  • Milk

Cobalah sendiri "

Lebih "Try it Yourself" contoh di bawah ini.


Definisi dan Penggunaan

The removeChild() metode menghapus node anak tertentu dari elemen tertentu.

Mengembalikan simpul yang dihapus sebagai objek Node, atau null jika node tidak ada.

Catatan: node anak yang dihapus tidak lagi bagian dari DOM. Namun, dengan referensi dikembalikan oleh metode ini, adalah mungkin untuk memasukkan anak dipindahkan ke elemen di lain waktu (See "More Examples") .

Tip: Gunakan appendChild() atau insertBefore() metode untuk memasukkan node yang dihapus ke dalam dokumen yang sama. Untuk masukkan ke dokumen lain, menggunakan dokumen. adoptNode() atau dokumen. importNode() metode.


Dukungan Browser

metode
removeChild() iya nih iya nih iya nih iya nih iya nih

Sintaksis

Nilai parameter

Parameter Mengetik Deskripsi
node Node object Wajib. Node objek yang ingin Anda hapus

Rincian teknis

Kembali Nilai: Sebuah objek Node, mewakili node dihapus, atau null jika node tidak ada
DOM Versi Inti Level 1 Node Obyek

Penggunaan fungsi REMOVECHILD pada PHP

Contoh lebih

Contoh

Cari tahu apakah daftar memiliki setiap node anak. Jika demikian, menghapus node anak pertama (index 0) :

// Get the <ul> element with id="myList"
var list = document.getElementById("myList");

// If the <ul> element has any child nodes, remove its first child node
if (list.hasChildNodes()) {
    list.removeChild(list.childNodes[0]);
}

Sebelum menghapus:

  • Coffee
  • Tea
  • Milk

Sebelum menghapus:

  • Tea
  • Milk

Cobalah sendiri "


Contoh

Hapus semua node anak dari daftar:

// Get the <ul> element with id="myList"
var list = document.getElementById("myList");

// As long as <ul> has a child node, remove it
while (list.hasChildNodes()) {  
    list.removeChild(list.firstChild);
}

Sebelum menghapus:

  • Coffee
  • Tea
  • Milk

Setelah menghapus:

Cobalah sendiri "


Contoh

Menghapus <li> elemen dengan id = "myLI" dari elemen induknya (tanpa menentukan simpul induknya):

var item = document.getElementById("myLI");
item.parentNode.removeChild(item);

Sebelum menghapus:

  • Coffee
  • Tea
  • Milk

Setelah menghapus:

  • Coffee
  • Milk

Cobalah sendiri "


Contoh

Menghapus <li> elemen dari induknya, dan masukkan lagi:

var item = document.getElementById("myLI");

function removeLi() {
    item.parentNode.removeChild(item);
}

function appendLi() {
    var list = document.getElementById("myList");
    list.appendChild(item);
}

Cobalah sendiri "


Contoh

Menghapus <span> elemen dari induknya dan masukkan ke <h2> elemen dalam dokumen lain:

var child = document.getElementById("mySpan");

function removeLi() {
    child.parentNode.removeChild(child);
}

function myFunction() {
    var frame = document.getElementsByTagName("IFRAME")[0]
    var h = frame.contentWindow.document.getElementsByTagName("H1")[0];
    var x = document.adoptNode(child);
    h.appendChild(x);
}

Cobalah sendiri "


<Elemen Object

(PHP 5, PHP 7, PHP 8)

DOMNode::removeChild Removes child from list of children

Description

public DOMNode::removeChild(DOMNode $child): DOMNode|false

Parameters

child

The removed child.

Return Values

If the child could be removed the function returns the old child.

Errors/Exceptions

DOM_NO_MODIFICATION_ALLOWED_ERR

Raised if this node is readonly.

DOM_NOT_FOUND

Raised if child is not a child of this node.

Examples

The following example will delete the chapter element of our XML document.

Example #1 Removing a child

<?php

$doc

= new DOMDocument;
$doc->load('book.xml');$book $doc->documentElement;// we retrieve the chapter and remove it from the book
$chapter $book->getElementsByTagName('chapter')->item(0);
$oldchapter $book->removeChild($chapter);

echo

$doc->saveXML();
?>

The above example will output:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN" 
          "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd">
<book id="listing">
 <title>My lists</title>
 
</book>

See Also

  • DOMChildNode::remove() - Removes the node
  • DOMNode::appendChild() - Adds new child at the end of the children
  • DOMNode::replaceChild() - Replaces a child

crazy888s at hotmail dot com

13 years ago

You can't remove DOMNodes from a DOMNodeList as you're iterating over them in a foreach loop. For example...

<?php
$domNodeList
= $domDocument->getElementsByTagname('p');
foreach (
$domNodeList as $domElement ) {
 
//  ...do stuff with $domElement...
 
$domElement->parentNode->removeChild($domElement);
}
?>

... will seemingly leave the internal iterator on the foreach out of wack and results will be quite strange. Though, making a queue of items to remove seems to work. For example...

<?php
$domNodeList
= $domDocument->getElementsByTagname('p');
$domElemsToRemove = array();
foreach (
$domNodeList as $domElement ) {
 
// ...do stuff with $domElement...
 
$domElemsToRemove[] = $domElement;
}
foreach(
$domElemsToRemove as $domElement ){
 
$domElement->parentNode->removeChild($domElement);
}
?>

vsematika at centrum dot cz

17 years ago

For those who don't understand >sbeam at onsetcorps dot net on 02-Feb-2005 12:07< 'hack', here's a little discussion:

First but *wrong* try would be:
<?php
foreach ($parent->childNodes as $child) {
  
$parent->removeChild($child);
?>
This doesn't work because DOM tree id modified on-the-fly and this confuses foreach loop.

The idea behind sbeam's trick is that after removing the first item in the first iteration, the second item in childNodes nodelist immediately becomes the first item. That's why we must _always_ remove the first child. Here's another implementation:

<?php
$count
= $parent->childNodes->length;
for (
$i = 0; $i < $count; $i++) {
  
$oldNode = $parent->removeChild($parent->childNodes->item(0)); // !!! not item($i) !!!
}
?>

Justin Sheckler

13 years ago

These two functions might be helpful to anyone trying to delete a node and all of its children:

<?php
function deleteNode($node) {
   
deleteChildren($node);
   
$parent = $node->parentNode;
   
$oldnode = $parent->removeChild($node);
}

function

deleteChildren($node) {
    while (isset(
$node->firstChild)) {
       
deleteChildren($node->firstChild);
       
$node->removeChild($node->firstChild);
    }
}
?>

Justin Carlson

12 years ago

Just a note, if you remove a node the whitespace it occupied will remain if you save the file.

To avoid this, use $document->preserveWhiteSpace = false;

For Example:

<?php
$document
= new DomDocument();
$document->preserveWhiteSpace = false;
?>

Kenney

9 years ago

This method will remove all child nodes but leave the attributes (and namespace declarations) intact.

<?php
function removeChildren( &$node )
{
 
$node->parentNode->replaceChild(
   
$n = $node->cloneNode( false ),
   
$node );$node = $n;
}
?>

Michael D (DigitalGemstones.com)

14 years ago

Note that iterating through the childNodes array and removing those children will stop the iteration.

For example:
<?php
foreach($node->childNodes as $child)
{
  if(
iWantToDeleteThisNode($child))
    
$child->parentNode->removeChild($child);
}
?>

Will not work.  Note that in removing the node, the childNodes array gets rebuilt (it seems) and so only the first item will be removed.  In order to properly remove all the children you want to remove, you will need to do something like the following:

<?php
$nodesToDelete
= array();
foreach(
$node->childNodes as $child)
{
  if(
iWantToDeleteThisNode($child))
   
$nodesToDelete[] = $child;
}
foreach(
$nodesToDelete as $node)
{
 
$node->parentNode->removeChild($node);
}
?>

I believe this is actually more efficient than the first snippet would be (if it worked) but obviously I cannot benchmark it.

nggit@phpindonesia

3 years ago

You can use foreach to remove multiple elements from a DOMNodeList, only if you use a temporary array or via XPath query.
If not, the simplest way will be:

<?phpwhile ($element = $nodeList->item(0)) {
   
$element->parentNode->removeChild($element);
}
?>

Wala

14 years ago

I used the following to delete an entire node using the child element:

<?php
$childElement
->parentNode->parentNode-> removeChild($childElement->parentNode);
?>

I could not have thought of it without the help of everyone who contributed to this thread.

Thanks!

(I had to introduce the extra space between parentNode->removeChild to get rid of the wordwrap() warning while posting this note)

blaine (at) blainegarrett (dot com)

17 years ago

This might be obvious, but might help someone as well...

If you simply have a node that you need to remove (perhaps from an xpath) and don't know the parent node offhand (the examples seem to assume you have the parent in a variable), then you can do something easy like this...

$node->parentNode->removeChild($node);

parentNode is a scalar property of the Element.

Hope that helps.

--
Blaine Garrett

drimsun at gmail dot com

12 years ago

If you try to remove children of a nested node (e.g. a <ul> list that contains another <ul>) and only remove the first level of child nodes, you'll get a messed-up node object.

Upon re-use of the messed-up node you'll get an error message that says: "Fatal error: Call to a member function blahblah() on a non-object".

To remove ALL child nodes, use this recursive function instead:
<?php
/**
* Helper function that removes all child nodes of $node recursively
* (i.e. including child nodes of the child nodes and so forth).
*/
function remove_children(&$node) {
  while (
$node->firstChild) {
    while (
$node->firstChild->firstChild) {
     
remove_children($node->firstChild);
    }
   
$node->removeChild($node->firstChild);
  }
}
?>

tim cameron ryan

14 years ago

Or something like:

<?phpwhile ($node->childNodes->length)
    
$node->removeChild($node->firstChild);?>

pedro at leb dot usp dot br

17 years ago

You may also use the hasChild function:
<?phpwhile($node->hasChildNodes()) {
 
$node->removeChild($node->childNodes->item(0));
}
?>

When you remove a childNode, the next node becomes the first one!

Vasil Rangelov

14 years ago

At the time of writing, I suppose rightfully, removeChild() removes only the selected node, but when you remove an element, it's child elements are not removed. If you want to achieve that, replaceChild() is the solution.

The following should remove all descendants of the $node DOMNode, regardless of it's name:
<?php
$node
->replaceChild(new DOMElement($node->nodeName), $node);
?>
If you're replacing the root element, you must explicitly state that with $node->documentElement as the second argument.

mytto at openxtrem dot com

17 years ago

Back again on removing childs and iterators robustness.

Things get a bit more complicated when you only want to remove 'some' nodes according to a certain condition. Then you can't just remove the first one repeatedly.

The trick is to copy the content of the node list into a more robust collection than DOMNodeList, I name array!

The following piece of code will, for instance, remove all empty child nodes:

<?php
// Copy childNodes array
$childNodes = array();
foreach(
$node->childNodes as $childNode) {
 
$childNodes[] = $childNode;
}
// Browse with the copy    
foreach ($childNodes as $childNode) {
  if (!
$childNode->hasChildNodes()); {
   
$childNode->parentNode->removeChild($childNode);
  }
}
?>