JavaScript - 要素を取得する: ParentNode.previousElementSibling
要素を取得する: ParentNode.previousElementSibling
ParentNode.previousElementSibling
プロパティは、指定したノードのすぐ前の要素を返します。指定したノードがリストの最後の場合は null
を返します。
/* HTML 側 */
<ul id="foo">
<li>First (1)</li>
<li>Second (2)</li>
<li>Third (3)</li>
</ul>
/* JavaScript 側 */
var foo = document.getElementById('foo');
console.log(foo.lastElementChild.previousElementSibling.textContent); // Second (2)
ParentNode.previousElementSibling
プロパティ