From 42b9b23c17200b2eea8b3b2b4e3b8bf2b6eae458 Mon Sep 17 00:00:00 2001 From: Masahiro FUJIMOTO Date: Sun, 19 Sep 2021 23:30:21 +0900 Subject: Document.querySelector および .querySelectorAll を更新 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Markdown に変換 - 2021/09/15 時点の英語版に同期 --- files/ja/web/api/document/queryselector/index.md | 109 +++++------ .../ja/web/api/document/queryselectorall/index.md | 214 +++++++++------------ 2 files changed, 145 insertions(+), 178 deletions(-) (limited to 'files/ja') diff --git a/files/ja/web/api/document/queryselector/index.md b/files/ja/web/api/document/queryselector/index.md index c0af97a434..7847ab82b7 100644 --- a/files/ja/web/api/document/queryselector/index.md +++ b/files/ja/web/api/document/queryselector/index.md @@ -15,57 +15,51 @@ tags: translation_of: Web/API/Document/querySelector browser-compat: api.Document.querySelector --- -
{{ApiRef("DOM")}}
+{{ApiRef("DOM")}} -

{{domxref("Document")}} の querySelector() メソッドは、指定されたセレクターまたはセレクターのグループに一致する、文書内の最初の {{domxref("Element")}} を返します。一致するものが見つからない場合は null を返します。

+{{domxref("Document")}} の **`querySelector()`** メソッドは、指定されたセレクターまたはセレクターのグループに一致する、文書内の最初の {{domxref("Element")}} を返します。一致するものが見つからない場合は `null` を返します。 -
-

メモ: 比較処理は、文書マークアップにおける最初の要素を経由して文書ノードの深さ優先前順走査 (depth-first pre-order traversal) を使用して実行され、子ノードのカウント順で順次ノードを反復して行われます。

-
+> **Note:** 照合処理は、文書マークアップにおける最初の要素を経由して文書ノードの深さ優先前順走査 (depth-first pre-order traversal) を使用して実行され、子ノードのカウント順で順次ノードを反復して行われます。 -

構文

+## 構文 -
element = document.querySelector(selectors);
-
+```js +element = document.querySelector(selectors); +``` -

引数

+### 引数 -
-
selectors
-
1 つまたは複数のセレクターを含む {{domxref("DOMString")}}。この文字列は妥当な CSS セレクターでなければならず、そうでない場合は SyntaxError が発生します。セレクターとその管理の方法の詳細について、セレクターを使用した DOM 要素の指定を参照してください。
-
+- _selectors_ + - : {{domxref("DOMString")}} で、照合する 1 つ以上のセレクターを設定します。この文字列は妥当な CSS セレクターでなければなりません。そうでない場合は `SyntaxError` が発生します。セレクターとその管理の方法の詳細について、[セレクターを使用した DOM 要素の指定](/ja/docs/Web/API/Document_object_model/Locating_DOM_elements_using_selectors)を参照してください。 -
-

メモ: 標準の CSS 構文に含まれない文字は、バックスラッシュ文字を使ってエスケープしなければなりません。JavaScript でもバックスラッシュのエスケープが使われているため、これらの文字を使った文字列リテラルを記述する際は、特に注意する必要があります。詳細は{{anch("Escaping special characters", "特殊文字のエスケープ")}}を参照してください。

-
+> **Note:** 標準の CSS 構文に含まれない文字は、バックスラッシュ文字を使ってエスケープしなければなりません。 JavaScript でもバックスラッシュによるエスケープが使われているため、これらの文字を使った文字列リテラルを記述する際は、特に注意する必要があります。詳細は[特殊文字のエスケープ](/ja/docs/Web/API/Document/querySelector#特殊文字のエスケープ)を参照してください。 -

返値

+### 返値 -

文書内で指定された CSS セレクターに最初に一致する要素を示す {{domxref("Element")}} オブジェクト、もしくは、一致する要素がない場合は null を返します。

+{{domxref("Element")}} オブジェクトで、文書内で指定された [CSS セレクター](/ja/docs/Web/CSS/CSS_Selectors)に最初に一致する要素を示すオブジェクト、もしくは、一致する要素がない場合は `null` を返します。 -

指定されたセレクターに一致するすべての要素のリストが必要な場合は、代わりに {{domxref("Document.querySelectorAll", "querySelectorAll()")}} を使用してください。

+指定されたセレクターに一致するすべての要素のリストが必要な場合は、代わりに {{domxref("Document.querySelectorAll", "querySelectorAll()")}} を使用してください。 -

例外

+### 例外 -
-
SyntaxError
-
指定された selectors の構文が妥当ではない。
-
+- `SyntaxError` + - : 指定された _selectors_ の構文が妥当ではない。 -

使用上のメモ

+## 使用上のメモ -

指定されたセレクターが、誤って文書内で複数回使われている ID に一致する場合は、その ID を持つ最初の要素が返されます。

+指定されたセレクターが、誤って文書内で複数回使われている ID に一致する場合は、その ID を持つ最初の要素が返されます。 -

CSS 擬似要素Selectors API で策定されている通り、何も要素を返しません。

+[CSS 擬似要素](/ja/docs/Web/CSS/Pseudo-elements)は [Selectors API](https://www.w3.org/TR/selectors-api/#grammar) で策定されている通り、何も要素を返しません。 -

特殊文字のエスケープ

+### 特殊文字のエスケープ -

標準の CSS の構文に従っていない ID やセレクター (例えば、コロンやスペースを不適切に使用しているもの) で一致させるためには、バックスラッシュ ("\") でその文字をエスケープしなければなりません。バックスラッシュは JavaScript のエスケープ文字でもあるので、文字列リテラルを入力する場合、それを 2 回エスケープする必要があります (1 回目は JavaScript の文字列のため、2 回目は querySelector() のため)。

+標準の CSS の構文に従っていない ID やセレクター (例えば、コロンや空白を不適切に使用しているもの) に一致させるためには、バックスラッシュ ("`\`") でその文字をエスケープしなければなりません。バックスラッシュは JavaScript のエスケープ文字でもあるので、文字列リテラルを入力する場合、それを *2 回*エスケープする必要があります (1 回目は JavaScript の文字列のため、2 回目は `querySelector()` のため)。 -
<div id="foo\bar"></div>
-<div id="foo:bar"></div>
+```html
+
+
-<script> + +``` -

+## 例 -

あるクラスに一致する最初の要素を探索する

+### あるクラスに一致する最初の要素を探索する -

次の例は、クラス "myclass" を持つ文書内の要素の内、最初のものを返します。

+次の例は、クラス "`myclass`" を持つ文書内の要素の内、最初のものを返します。 -
var el = document.querySelector(".myclass");
-
+```js +var el = document.querySelector(".myclass"); +``` -

より複雑なセレクター

+### より複雑なセレクター -

セレクターは、次の例で示しているように、実に力強いものになり得ます。ここでは、文書内でクラスが "user-panel main" である {{HTMLElement("div")}} (<div class="user-panel main">) の中にある、"login" という名前を持つ最初の {{HTMLElement("input")}} 要素 (<input name="login"/>) が返されます。

+セレクターは、次の例で示しているように、実に力強いものになり得ます。ここでは、文書内で {{HTMLElement("input")}} に "login" という名前の付いた最初のもの (``) のうち、 {{HTMLElement("div")}} でクラスが "user-panel main" (`
`) の中にあるものが返されます。 -
var el = document.querySelector("div.user-panel.main input[name='login']");
-
+```js +var el = document.querySelector("div.user-panel.main input[name='login']"); +``` -

否定

+### 否定 -

すべての CSS セレクター文字列が正しい場合、セレクターを否定することもできます。

+すべての CSS セレクター文字列が妥当な場合、セレクターを否定することもできます。 -
var el = document.querySelector("div.user-panel:not(.main) input[name='login']");
+```js +var el = document.querySelector("div.user-panel:not(.main) input[name='login']"); +``` -

これで、input 要素のうち親に user-panel クラスのついた div があるものの、main クラスがないものを 1 つ選択します。

+これで、input 要素のうち親に `user-panel` クラスのついた div があるものの、`main` クラスがないものを 1 つ選択します。 -

仕様書

+## 仕様書 {{Specifications}} -

ブラウザーの互換性

+## ブラウザーの互換性 -
{{Compat}}
+{{Compat}} -

関連情報

+## 関連情報 - +- [セレクターを使用した DOM 要素の指定](/ja/docs/Web/API/Document_object_model/Locating_DOM_elements_using_selectors) +- {{domxref("Element.querySelector()")}} +- {{domxref("Document.querySelectorAll()")}} +- {{domxref("Element.querySelectorAll()")}} diff --git a/files/ja/web/api/document/queryselectorall/index.md b/files/ja/web/api/document/queryselectorall/index.md index 29d3513ec2..56cab2c7da 100644 --- a/files/ja/web/api/document/queryselectorall/index.md +++ b/files/ja/web/api/document/queryselectorall/index.md @@ -3,176 +3,146 @@ title: Document.querySelectorAll() slug: Web/API/Document/querySelectorAll tags: - API - - CSS Selectors + - CSS セレクター - DOM - Document - Finding Elements - Locating Elements - - Method - - Reference + - メソッド + - リファレンス - Searching Elements - Selecting Elements - - Selectors + - セレクター - querySelectorAll - - メソッド +browser-compat: api.Document.querySelectorAll translation_of: Web/API/Document/querySelectorAll --- -
{{APIRef("DOM")}}
+{{APIRef("DOM")}} -

{{domxref("Document")}} の querySelectorAll() メソッドは、与えられた CSS セレクターに一致する文書中の要素のリストを示す静的な (生きていない) {{domxref("NodeList")}} を返します。

+{{domxref("Document")}} の **`querySelectorAll()`** メソッドは、与えられた CSS セレクターに一致する文書中の要素のリストを示す静的な (ライブではない) {{domxref("NodeList")}} を返します。 -
-

メモ: このメソッドは {{domxref("ParentNode")}} ミックスインの {{domxref("ParentNode.querySelectorAll", "querySelectorAll()")}} メソッドを元に実装されています。

-
+## 構文 -

構文

+```js +elementList = parentNode.querySelectorAll(selectors); +``` -
elementList = parentNode.querySelectorAll(selectors);
-
+### 引数 -

引数

+- `selectors` + - : {{domxref("DOMString")}} で、照合対象となる 1 つまたは複数のセレクターを含みます。この文字列は妥当な [CSS セレクター](/ja/docs/Web/CSS/CSS_Selectors)でなければならず、そうでない場合は `SyntaxError` 例外が発生します。セレクターの仕様と要素の識別の詳細は、[セレクターを使用した DOM 要素の指定](/ja/docs/Web/API/Document_object_model/Locating_DOM_elements_using_selectors)を参照してください。複数のセレクターは、カンマで区切って指定することができます。 -
-
selectors
-
マッチのための 1 つまたは複数のセレクターを含む {{domxref("DOMString")}}。この文字列は妥当な CSS セレクターでなければならず、そうでない場合は SyntaxError 例外がスローされます。セレクターの仕様と要素の識別の詳細は、セレクターを使用した DOM 要素の指定を参照してください。複数のセレクターを指定する際は、カンマで区切ります。
-
+> **Note:** 標準の CSS 構文に含まれない文字は、バックスラッシュ文字を使ってエスケープしなければなりません。 JavaScript でもバックスラッシュによるエスケープが使われているため、これらの文字を使った文字列リテラルを記述する際は、特に注意する必要があります。詳細は[特殊文字のエスケープ](/ja/docs/Web/API/Document/querySelector#特殊文字のエスケープ)を参照してください。 -
-

メモ: 標準の CSS 構文の一部ではない文字は、バックスラッシュ文字を使ってエスケープしなければなりません。 JavaScript でもバックスラッシュによるエスケープが使われているため、これらの文字を使った文字列リテラルを記述する際は、特に注意する必要があります。詳細は {{anch("Escaping special characters")}} を参照してください。

-
+### 返値 -

返値

+ライブではない {{domxref("NodeList")}} で、指定されたセレクターの少なくとも 1 つに一致する要素ごとに {{domxref("Element")}} を一つずつ含みます。または一致するものがなければ空の {{domxref("NodeList")}} です。 -

指定されたセレクターの少なくとも一つに一致する要素ごとに {{domxref("Element")}} を一つずつ含む、生きていない {{domxref("NodeList")}}、または一致するものがなければ空の {{domxref("NodeList")}} です。

+> **Note:** 指定された `selectors` が [CSS 擬似要素](/ja/docs/Web/CSS/Pseudo-elements)を含む場合、返されるリストは常に空になります。 -
-

メモ: 指定された selectorsCSS 擬似要素を含む場合、返されるリストは常に空になります。

-
- -

例外

+### 例外 -
-
SyntaxError
-
指定された selectors の構文が妥当ではない。
-
+- `SyntaxError` + - : 指定された `selectors` の構文が妥当ではない場合です。 -

+## 例 -

一致のリストの入手

+### 一致するもののリストの入手 -

文書内のすべての {{HTMLElement("p")}} 要素の {{domxref("NodeList")}} を入手します。

+文書内のすべての {{HTMLElement("p")}} 要素の {{domxref("NodeList")}} を入手します。 -
var matches = document.querySelectorAll("p");
+```js +const matches = document.querySelectorAll("p"); +``` -

次の例では、文書内にある note または alert のいずれかのクラスを持つ、すべての {{HTMLElement("div")}} 要素のリストを返します。

+次の例では、文書内にあるすべての {{HTMLElement("div")}} 要素のうち、 `note` または `alert` のいずれかのクラスを持つものリストを返します。 -
var matches = document.querySelectorAll("div.note, div.alert");
-
+```js +const matches = document.querySelectorAll("div.note, div.alert"); +``` -

次に、 test という ID を持つコンテナ内に位置し、直接の親要素が highlighted クラスを持つ {{HTMLElement("div")}} である、<p> 要素のリストを取得します。

+次に、 `

` 要素ののうち直近の親要素が `test` という ID を持つコンテナー内に位置し、直接の親要素が `highlighted` クラスを持つ {{HTMLElement("div")}} であるリストを取得します。 -

var container = document.querySelector("#test");
-var matches = container.querySelectorAll("div.highlighted > p");
+```js +const container = document.querySelector("#test"); +const matches = container.querySelectorAll("div.highlighted > p"); +``` -

次の例では属性セレクターを使用しており、 data-src という名前の属性を持つ、文書内の {{HTMLElement("iframe")}} 要素のリストを返します。

+次の例では[属性セレクター](/ja/docs/Web/CSS/Attribute_selectors)を使用しており、 `data-src` という名前の属性を持つ、文書内の {{HTMLElement("iframe")}} 要素のリストを返します。 -
var matches = document.querySelectorAll("iframe[data-src]");
+```js +const matches = document.querySelectorAll("iframe[data-src]"); +``` -

次の例では、ID が "userlist" の要素の中にあり、"data-active" 属性を持ち、その値が "1" であるリスト項目のリストを返すため、属性セレクターが使用されています。

+次の例では、ID が `userlist` の要素の中にあり、`data-active` 属性を持ち、その値が `1` であるリスト項目のリストを返すため、属性セレクターが使用されています。 -
var container = document.querySelector("#userlist");
-var matches = container.querySelectorAll("li[data-active='1']");
+```js +const container = document.querySelector("#userlist"); +const matches = container.querySelectorAll("li[data-active='1']"); +``` -

一致したリストへのアクセス

+### 一致したリストへのアクセス -

一旦、一致した要素の {{domxref("NodeList")}} が返されると、それをちょうど配列のように試すことができます。配列が空である (length プロパティが 0 である) 場合は、一致がなかったということです。

+いったん、一致した要素の {{domxref("NodeList")}} が返されると、それをちょうど配列のように見ることができます。配列が空である (`length` プロパティが 0 である) 場合は、一致がなかったということです。 -

それ以外の場合は、単純に標準の配列表記を使って、リストの内容にアクセスすることができます。次のように、任意の一般的なループ処理を使うことができます。

+それ以外の場合は、単純に標準の配列表記を使って、リストの内容にアクセスすることができます。次のように、任意の一般的なループ処理を使うことができます。 -
var highlightedItems = userList.querySelectorAll(".highlighted");
+```js
+const highlightedItems = userList.querySelectorAll(".highlighted");
 
 highlightedItems.forEach(function(userItem) {
   deleteUser(userItem);
-});
+}); +``` -

ユーザーのメモ

+## ユーザーのメモ -

querySelectorAll() は、最も一般的な JavaScript DOM ライブラリと異なる動作を持ち、意図しない結果をもたらすことがあります。

+`querySelectorAll()` は、最も一般的な JavaScript DOM ライブラリーと異なる動作を持ち、意図しない結果をもたらすことがあります。 -

HTML

+### HTML -

次の、入れ子になった 3 つの {{HTMLElement("div")}} ブロックを持つ HTML について検討します。

+次の、入れ子になった 3 つの {{HTMLElement("div")}} ブロックを持つ HTML について検討します。 -
<div class="outer">
-  <div class="select">
-    <div class="inner">
-    </div>
-  </div>
-</div>
+```html +
+
+
+
+
+
+``` -

JavaScript

+### JavaScript -
var select = document.querySelector('.select');
-var inner = select.querySelectorAll('.outer .inner');
+```js
+const select = document.querySelector('.select');
+const inner = select.querySelectorAll('.outer .inner');
 inner.length; // 1 です。0 ではありません!
-
+``` -

この例では、"select" class を持つ <div> の文脈で ".outer .inner" を選択するとき、.outer が基準となる要素(.select で検索される)の子孫ではないにもかかわらず、".inner" class を持つ要素が見つけられています。querySelectorAll() はデフォルトでは、セレクターの最後の要素が検索スコープに含まれているかどうかのみ検証します。

+この例で、`.outer .inner` を`
` に `select` クラスがついたコンテキストで選択する場合、`.outer` が検索が行われる基底要素 (`.select`) の子孫ではないにもかかわらず、`.inner` クラスの要素が見つかります。既定では、`querySelectorAll()` はセレクターの最後の要素のみを、検索スコープ内にあるかどうか検証します。 -

{{cssxref(":scope")}} 擬似クラスを使うと、基準となる要素の子孫だけが一致するようになり、期待される挙動を取り戻すことができます。

+{{cssxref(":scope")}} 擬似クラスを使うと、基準となる要素の子孫だけが一致するようになり、期待される挙動を取り戻すことができます。 -
var select = document.querySelector('.select');
-var inner = select.querySelectorAll(':scope .outer .inner');
+```js
+const select = document.querySelector('.select');
+const inner = select.querySelectorAll(':scope .outer .inner');
 inner.length; // 0
-
- -

仕様書

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
仕様書状態備考
{{SpecName("DOM WHATWG", "#dom-parentnode-queryselectorall", "ParentNode.querySelectorAll()")}}{{Spec2("DOM WHATWG")}}Living standard
{{SpecName("Selectors API Level 2", "#dom-parentnode-queryselectorall", "ParentNode.querySelectorAll()")}}{{Spec2("Selectors API Level 2")}}変更なし
{{SpecName("DOM4", "#dom-parentnode-queryselectorall", "ParentNode.querySelectorAll()")}}{{Spec2("DOM4")}}初回定義
{{SpecName("Selectors API Level 1", "#interface-definitions", "document.querySelector()")}}{{Spec2("Selectors API Level 1")}}独自の定義
- -

ブラウザーの互換性

- -

{{Compat("api.Document.querySelectorAll")}}

- -

関連情報

- - +``` + +## 仕様書 + +{{Specifications}} + +## ブラウザーの互換性 + +{{Compat}} + +## 関連情報 + +- [セレクターを使用した DOM 要素の指定](/ja/docs/Web/API/Document_object_model/Locating_DOM_elements_using_selectors) +- CSS ガイドの[[属性セレクター]](/ja/docs/Web/CSS/Attribute_selectors) +- MDN 学習領域の[[属性セレクター]](/ja/docs/Learn/CSS/Building_blocks/Selectors/Attribute_selectors) +- {{domxref("Element.querySelector()")}} および {{domxref("Element.querySelectorAll()")}} +- {{domxref("Document.querySelector()")}} +- {{domxref("DocumentFragment.querySelector()")}} および {{domxref("DocumentFragment.querySelectorAll()")}} -- cgit v1.2.3-54-g00ecf