طريقة XML DOM removeChild()
التعريف والاستخدام
يمكن للطريقة removeChild() إزالة نود معينة من قائمة النود الابنية.
إذا تم الحذف بنجاح، يمكن لهذه الطريقة العودة بالنود المزالة، وإذا لم يحدث ذلك، فإنها تعود NULL.
النحو:
nodeObject.removeChild(node)
الم参数 | وصف |
---|---|
node | مطلوب. |
تنبيهات وتعليقات
تعليق:سيقوم Internet Explorer بتخطي النود الفارغة التي تنتج بين النود (مثل علامة النسخة)، بينما لن يفعل ذلك Mozilla. لذا، سنستخدم في المثال التالي دالة لتحقق من نوع النود الأخيرة.
نوع النود للنود الإлементية هو 1، لذا إذا لم يكن العنصر الأول من النود هو نود إлементي، فإنه سيقوم بالانتقال إلى النود التالية، ثم يستمر في التحقق مما إذا كانت هذه النود هي نود إлементية. هذه العملية ستستمر حتى يتم العثور على العنصر الأول من النود الإлементية. بهذه الطريقة، يمكننا الحصول على الطريقة الصحيحة في Internet Explorer وMozilla.
تنبيه:For more information on the differences between XML DOM in IE and Mozilla browsers, please visit our DOM browser chapter.
example
In all examples, we will use the XML file books.xml, and the JavaScript function loadXMLDoc().
The following code snippet can be used to delete the last child node from the first <book> element:
//check if last child node is an element node
function get_lastchild(n)
{
var x=n.lastChild;
while (x.nodeType!=1)
{
x=x.previousSibling;
}
return x;
}
xmlDoc=loadXMLDoc("books.xml");
var x=xmlDoc.getElementsByTagName("book")[0];
deleted_node=x.removeChild(get_lastchild(x))
;
document.write("Node removed: " + deleted_node.nodeName);
Output:
Node removed: price