Node.previousSibling
La propiedad de sólo-lectura Node.previousSibling
devuelve el nodo inmediatamente anterior al especificado en la lista de nodos childNodes
de su padre, o null
si el nodo especificado es el primero en dicha lista.
Sintaxis
nodoAnterior = nodo.previousSibling;
Ejemplo
<img id="b0">
<img id="b1">
<img id="b2">
console.log(document.getElementById("b1").previousSibling); // <img id="b0">
console.log(document.getElementById("b2").previousSibling.id); // "b1"
Notas
Gecko-based browsers insert text nodes into a document to represent whitespace in the source markup.
Therefore a node obtained, for example, using Node.firstChild (en-US) or Node.previousSibling
may refer to a
whitespace text node rather than the actual element the author intended to get.
See Whitespace in the DOM and W3C DOM 3 FAQ: Why are some Text nodes empty? for more information.
Para navegar en el sentido opuesto de la lista de nodos hijos se utiliza Node.nextSibling.
Especificación
Compatibilidad con navegadores
BCD tables only load in the browser