字符数据:previousElementSibling 属性

CharacterData 接口的只读 **previousElementSibling** 属性返回当前节点在其父节点的子节点列表中之前的第一个 Element,如果不存在则返回 null

Element 对象,如果未找到兄弟节点则返回 null

示例

html
<div id="div-01">Here is div-01</div>
TEXT
<div id="div-02">Here is div-02</div>
SOME TEXT
<div id="div-03">Here is div-03</div>
<pre>Result</pre>
js
// Initially set node to the Text node with `SOME TEXT`
let node = document.getElementById("div-02").nextSibling;

let result = "Previous element siblings of SOME TEXT:\n";

while (node) {
  result += `${node.nodeName}\n`;
  node = node.previousElementSibling;
}

document.querySelector("pre").textContent = result;

规范

规范
DOM 标准
# ref-for-dom-nondocumenttypechildnode-previouselementsibling②

浏览器兼容性

BCD 表格仅在浏览器中加载

另请参见