CharacterData: previousElementSibling プロパティ

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

previousElementSiblingCharacterData インターフェイスの読み取り専用プロパティで、親の子リストの中で、現在のノードの一つ前の Element を返します。存在しなければ null を返します。

Element オブジェクト、または兄弟ノードが見つらなければ null です。

html
<div id="div-01">これは div-01 です</div>
TEXT
<div id="div-02">これは div-02 です</div>
SOME TEXT
<div id="div-03">これは div-03 です</div>
<pre>結果</pre>
js
// 最初に node に Text ノード `SOME TEXT` を設定
let node = document.getElementById("div-02").nextSibling;

let result = "SOME TEXT の前の兄弟要素:\n";

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

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

仕様書

Specification
DOM
# ref-for-dom-nondocumenttypechildnode-previouselementsibling②

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
previousElementSibling

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

関連情報