Element: previousElementSibling 屬性
Element.previousElementSibling 只讀屬性返回在父元素的 children 列表中緊鄰指定元素之前的那個 Element;如果指定元素是列表中的第一個元素,則返回 null。
值
一個 Element 物件,或者 null。
示例
html
<div id="div-01">Here is div-01</div>
<div id="div-02">Here is div-02</div>
<li>This is a list item</li>
<li>This is another list item</li>
<div id="div-03">Here is div-03</div>
js
let el = document.getElementById("div-03").previousElementSibling;
console.log("Siblings of div-03:");
while (el) {
console.log(el.nodeName);
el = el.previousElementSibling;
}
本示例在頁面載入時輸出以下內容:
Siblings of div-03: LI LI DIV DIV
規範
| 規範 |
|---|
| DOM # ref-for-dom-nondocumenttypechildnode-previouselementsibling② |
瀏覽器相容性
載入中…