DOM - Atributo do objeto do nó - previousSibling
O atributo previousSibling especifica o nó imediatamente anterior ao nó atual.
Sintaxe
A seguir está a sintaxe para o uso do atributo previousSibling .
nodeObject.previousSibling
Exemplo
O conteúdo do node.xml é o seguinte -
<?xml version = "1.0"?>
<Company>
<Employee category = "Technical">
<FirstName>Tanmay</FirstName>
<LastName>Patil</LastName>
<ContactNo>1234567890</ContactNo>
<Email>[email protected]</Email>
</Employee>
<Employee category = "Non-Technical">
<FirstName>Taniya</FirstName>
<LastName>Mishra</LastName>
<ContactNo>1234667898</ContactNo>
<Email>[email protected]</Email>
</Employee>
<Employee category = "Management">
<FirstName>Tanisha</FirstName>
<LastName>Sharma</LastName>
<ContactNo>1234562350</ContactNo>
<Email>[email protected]</Email>
</Employee>
</Company>
O exemplo a seguir demonstra o uso do atributo previousSibling -
<!DOCTYPE html>
<html>
<body>
<script>
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","/dom/node.xml",false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
function get_previousSibling(p) {
a = p.previousSibling;
while (a.nodeType != 1) {
a = a.previousSibling;
}
return a;
}
x = get_previousSibling(xmlDoc.getElementsByTagName("Email")[0]);
document.write("Previous sibling of Email is : ");
document.write(x.nodeName);
</script>
</body>
</html>
Execução
Salve este arquivo como nodeattribute_previoussibling.htm no caminho do servidor (este arquivo e node.xml devem estar no mesmo caminho em seu servidor). Obteremos a saída conforme mostrado abaixo -
Previous sibling of Email is : ContactNo