خصائص previousSibling في XML DOM
التعريف والاستخدام
previousSibling
يعود هذا العنصر إلى العنصر المسبق للمستوى نفسه للعنصر المحدد (العنصر المسبق في نفس مستوى الشجرة).
إذا لم يكن هناك مثل هذا العنصر، فإن هذا العنصر يعود null.
النحو
elementNode.previousSibling
ملاحظة:سيقوم Firefox ومعظم المتصفحات الأخرى بتقديم الفراغ أو المسافات البيضاء أو النسخات إلى النصوص النود، بينما لن يقوم Internet Explorer بذلك. لذلك، في المثال التالي، سنستخدم دالة لتحقق من نوع العنصر المسبق للمستوى نفسه.
م节点 nodeType هو 1، لذا إذا لم يكن العنصر المسبق للمستوى نفسه node ليس عنصرًا، فإنه ينتقل إلى العنصر المسبق ويحقق ما إذا كان العنصر هو عنصرًا. سيستمر هذا حتى يتم العثور على العنصر المسبق للمستوى نفسه (يجب أن يكون عنصرًا). بذلك، ستكون النتيجة صحيحة في جميع المتصفحات.
Tip:For more information on differences between browsers, please visit the DOM Browser section in the XML DOM tutorial.
Example
Example 1
The following code loads "books.xml" into xmlDoc and gets the previous sibling node of the first <author> element:
var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { myFunction(this); } }; xhttp.open("GET", "books.xml", true); xhttp.send(); // Check if the previous sibling node is an element node function get_previoussibling(n) { var x = n.previousSibling; while (x.nodeType != 1) { x = x.previousSibling; } return x; } function myFunction(xml) { var xmlDoc = xml.responseXML; var x = xmlDoc.getElementsByTagName("author")[0]; var y = get_previoussibling(x); document.getElementById("demo").innerHTML = x.nodeName + " = " + x.childNodes[0].nodeValue + "<br>Previous sibling: " + y.nodeName + " = " + y.childNodes[0].nodeValue; }
Example 2
Use nextSibling to get the next sibling node of the element:
var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { myFunction(this); } }; xhttp.open("GET", "books.xml", true); xhttp.send(); // Check if the next sibling node is an element node function get_nextsibling(n) { var x = n.nextSibling; while (x.nodeType != 1) { x = x.nextSibling; } return x; } function myFunction(xml) { var xmlDoc = xml.responseXML; var x = xmlDoc.getElementsByTagName("title")[0]; var y = get_nextsibling(x); document.getElementById("demo").innerHTML = x.nodeName + " = " + x.childNodes[0].nodeValue + "<br>اخوة السابق: " + y.nodeName + " = " + y.childNodes[0].nodeValue; }