From 074785cea106179cb3305637055ab0a009ca74f2 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:42:52 -0500 Subject: initial commit --- files/pl/web/api/document/queryselector/index.html | 159 +++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 files/pl/web/api/document/queryselector/index.html (limited to 'files/pl/web/api/document/queryselector') diff --git a/files/pl/web/api/document/queryselector/index.html b/files/pl/web/api/document/queryselector/index.html new file mode 100644 index 0000000000..cecd7c4f14 --- /dev/null +++ b/files/pl/web/api/document/queryselector/index.html @@ -0,0 +1,159 @@ +--- +title: Document.querySelector() +slug: Web/API/Document/querySelector +tags: + - API + - DOM + - Metodă + - Referencje + - Selektory +translation_of: Web/API/Document/querySelector +--- +
{{ ApiRef("DOM") }}
+ +

Funkcja zwraca pierwszy element wewnątrz dokumentu, który pasuje do podanego selektora lub grupy selektorów.

+ +

Składnia

+ +
element = document.querySelector(selectors);
+
+ +

gdzie

+ + + +

Przykład

+ +

W tym przykladzie zostaje zwrócony pierwszy element w dokumencie o klasie "myclass":

+ +
var el = document.querySelector(".myclass");
+
+ +

Przykład: Powerful

+ +

Selectory mogą być naprawdę potężne, jak pokazano na poniższym przykładzie. Pierwszy element <input name="login"/> wewnątrz <div class="user-panel main"> w dokumencie zostaje zwrócony:

+ +
var el = document.querySelector("div.user-panel.main input[name='login']");
+
+ +

Uwagi

+ +

Jeśli nie znaleziono dopasowań - zwraca null. W przeciwnym wypadku zwraca pierwszy pasujący element.

+ +

Jeśli selektor zawiera ID i to ID zostaje wielokrotnie błędnie użyte w dokumencie, zwracany jest pierwszy pasujący element.

+ +

Gdy podana grupa selektorów jest nieprawidłowa, zwrócony zostaje wyjątek SYNTAX_ERR.

+ +

querySelector() został wprowadzony w API Selektorów.

+ +

Przekazany do funkcji querySelector łańcuch znaków (string) musi być zgodny ze składnią CSS.

+ +

Zgodnie z API Selektorów pseudoklasy CSS nigdy nie zwrócą żadnego elementu.

+ +

Aby użyć ID lub selektorów niezgodnych ze składnią CSS (np. ze względu na niewłaściwe użycie dwukropka czy spacji) należy poprzedzić niezgodny znak ukośnikiem wstecznym (backslash). Z racji, że w JavaScript backslash jest używany jako znak ucieczki chcąc użyć go w stringu należy wpisać go podwójnie(jeden dla stringa JavaScriptu i drugi raz dla querySelector):

+ +
<div id="foo\bar"></div>
+<div id="foo:bar"></div>
+
+<script>
+  console.log('#foo\bar')               // "#fooar"
+  document.querySelector('#foo\bar')    // Nie pasuje do żadnego elementu
+
+  console.log('#foo\\bar')              // "#foo\bar"
+  console.log('#foo\\\\bar')            // "#foo\\bar"
+  document.querySelector('#foo\\\\bar') // Odnosi się do pierwszego diva
+
+  document.querySelector('#foo:bar')    // Nie pasuje do żadnego elementu
+  document.querySelector('#foo\\:bar')  // Odnosi się do drugiego diva
+</script>
+
+ +

Specyfikacje

+ + + + + + + + + + + + + + + + + + + +
SpecyfikacjaStatusKomentarz
{{SpecName("Selectors API Level 2", "#interface-definitions", "document.querySelector()")}}{{Spec2("Selectors API Level 2")}}
{{SpecName("Selectors API Level 1", "#interface-definitions", "document.querySelector()")}}{{Spec2("Selectors API Level 1")}}Definicja wstępna
+ +

Zgodność z przeglądarkami

+ +

{{CompatibilityTable()}}

+ +
+ + + + + + + + + + + + + + + + + + + +
CechaChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Wsparcie podstawowe13.5 (1.9.1)
+ {{bug(416317)}}
8103.2 (525.3)
+ {{Webkitbug("16587")}}
+
+ +
+ + + + + + + + + + + + + + + + + + + +
CechaAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Wsparcie podstawowe2.1yes910.03.2
+
+ + + +

Zobacz także

+ + + + -- cgit v1.2.3-54-g00ecf