1. 開発者向けのウェブ技術
  2. Web API
  3. Element
  4. previousElementSibling

このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。

View in English Always switch to English

Element: previousElementSibling プロパティ

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2015年7月⁩.

Element.previousElementSibling は読み取り専用のプロパティで、この要素 (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>
<script>
 let el = document.getElementById("div-03").previousElementSibling;
 document.write("<p>Siblings of div-03</p><ol>");
 while (el) {
 document.write("<li>" + el.nodeName + "</li>");
 el = el.previousElementSibling;
 }
 document.write("</ol>");
</script>

この例は読み込み時に、ページに以下のような出力を行います。

Siblings of div-03
 1. LI
 2. LI
 3. DIV
 4. DIV

仕様書

Specification
DOM
# ref-for-dom-nondocumenttypechildnode-previouselementsibling2

ブラウザーの互換性

関連情報

Help improve MDN

Learn how to contribute

This page was last modified on by MDN contributors.

AltStyle によって変換されたページ (->オリジナル) /