DOMNode::removeChild()函數是PHP中的內置函數,用於從子代列表中刪除子代。
用法:
DOMNode DOMNode::removeChild( DOMNode $oldnode )
參數:該函數接受單個參數$oldnode,該參數保存要刪除的孩子。
返回值:該函數成功返回被刪除的孩子。
異常:如果該節點是隻讀節點,則此函數將拋出DOM_NO_MODIFICATION_ALLOWED_ERR,如果$oldnode不是該節點的子節點,則此函數將拋出DOM_NOT_FOUND。
以下示例說明了PHP中的DOMNode::removeChild()函數:
範例1:
<?php
// Create a new DOMDocument instance
$document = new DOMDocument();
// Create a div element
$element = $document->
appendChild(new DOMElement('div'));
// Create a text Node
$text1 = $document->
createTextNode('GeeksforGeeks');
// Append the nodes
$element->appendChild($text1);
// Remove the child
$element->removeChild($text1);
// Render the XML
echo $document->saveXML();
?>
輸出:按Ctrl + U查看XML
範例2:
<?php
// Create a new DOMDocument instance
$document = new DOMDocument();
// Create a h1 element
$element = $document->
appendChild(new DOMElement('h1'));
// Create the text Node
$text1 = $document->
createTextNode('GeeksforGeeks');
$text2 = $document->
createTextNode('Text to be removed');
// Append the nodes
$element->appendChild($text1);
$element->appendChild($text2);
// Remove the child
$element->removeChild($text2);
// Render the output
echo $document->saveXML();
?>
輸出:
參考: https://www.php.net/manual/en/domnode.removechild.php
相關用法
- PHP DOMNode hasAttributes()用法及代碼示例
- PHP DOMNode lookupPrefix()用法及代碼示例
- PHP DOMNode lookupNamespaceUri()用法及代碼示例
- PHP DOMNode isSupported()用法及代碼示例
- PHP DOMNode isDefaultNamespace()用法及代碼示例
- PHP DOMNode hasChildNodes()用法及代碼示例
- PHP DOMNode getNodePath()用法及代碼示例
- PHP DOMNode normalize()用法及代碼示例
- PHP DOMNode appendChild()用法及代碼示例
- PHP DOMNode getLineNo()用法及代碼示例
- PHP DOMNode C14NFile()用法及代碼示例
- PHP DOMNode cloneNode()用法及代碼示例
- PHP DOMNode C14N()用法及代碼示例
- HTML DOM removeChild()用法及代碼示例
注:本文由純淨天空篩選整理自gurrrung大神的英文原創作品 PHP | DOMNode removeChild() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。