Esta página ha sido traducida del inglés por la comunidad. Aprende más y únete a la comunidad de MDN Web Docs.

View in English Always switch to English

Node.previousSibling

Baseline
Ampliamente disponible

Esta característica está consolidada y funciona en muchos dispositivos y versiones de navegador. Está disponible en todos los navegadores desde julio de 2015.

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

html
<img id="b0" />
<img id="b1" />
<img id="b2" />
js
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 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.

Especificaciones

Especificación
DOM
# ref-for-dom-node-previoussibling①

Compatibilidad con navegadores

Ver también