--- title: Element.closest() slug: Web/API/Element/closest tags: - API - DOM - Element - Method - Reference - メソッド translation_of: Web/API/Element/closest ---
closest() メソッドは、要素とその親 (文書ルートに向かって) を、指定されたセレクター文字列に一致するノードが見つかるまで探索します。自分自身または一致する祖先を返します。そのような要素が存在しない場合は、 null を返します。
var closestElement = targetElement.closest(selectors);
selectors は {{domxref("DOMString")}} で、セレクターのリストを指定します。p:hover, .toto + qclosestElement は選択された要素の直近の祖先に当たる {{domxref("Element")}} です。 null になることがあります。selectors が妥当なセレクターリストの文字列ではない場合、 {{exception("SyntaxError")}} が投げられます。<article>
<div id="div-01">Here is div-01
<div id="div-02">Here is div-02
<div id="div-03">Here is div-03</div>
</div>
</div>
</article>
var el = document.getElementById('div-03');
var r1 = el.closest("#div-02");
// id=div-02 である要素を返す
var r2 = el.closest("div div");
// div の中にある div である直近の祖先、ここでは div-03 自身を返す
var r3 = el.closest("article > div");
// 親に article を持つ div である直近の祖先、ここでは div-01 を返す
var r4 = el.closest(":not(div)");
// div ではない直近の祖先、ここではもっとも外側の article を返す
Element.closest() に対応していないブラウザーで、 element.matches() (または接頭辞付きの同等のもの、すなわち IE9+) に対応しているものには、ポリフィルがあります。
if (!Element.prototype.matches) {
Element.prototype.matches = Element.prototype.msMatchesSelector ||
Element.prototype.webkitMatchesSelector;
}
if (!Element.prototype.closest) {
Element.prototype.closest = function(s) {
var el = this;
do {
if (Element.prototype.matches.call(el, s)) return el;
el = el.parentElement || el.parentNode;
} while (el !== null && el.nodeType === 1);
return null;
};
}
しかし、本当に IE 8 の対応が必要な場合は、以下のポリフィルがとても遅い処理ながら、結果を出すことができます。但し、 IE 8 は CSS 2.1 のセレクターにしか対応しておらず、本番のウェブサイトが極端に遅くなる原因となることがあります。
if (window.Element && !Element.prototype.closest) {
Element.prototype.closest =
function(s) {
var matches = (this.document || this.ownerDocument).querySelectorAll(s),
i,
el = this;
do {
i = matches.length;
while (--i >= 0 && matches.item(i) !== el) {};
} while ((i < 0) && (el = el.parentElement));
return el;
};
}
| 仕様書 | 状態 | 備考 |
|---|---|---|
| {{SpecName('DOM WHATWG', '#dom-element-closest', 'Element.closest()')}} | {{Spec2('DOM WHATWG')}} | 初回定義 |
{{Compat("api.Element.closest")}}
document.createElement(tagName).closest(tagName) が null を返します。