English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML DOM querySelector() method

Objet Document HTML DOM

querySelector()Returns the first element in the document that matches the specified selector or selector group.

If no match is found, then return null.

The querySelector() method returns only the first element that matches the specified selector. To return all matches, please usequerySelectorAll()Method.

Syntax:

document.querySelector(selectors)
document.querySelector('h1');
Vérifiez et voyez‹/›

Browser compatibility

The numbers in the table specify the first browser version that fully supports the querySelector() method:

Method
querySelector()13.5103.28

Parameter value

ParameterDescription
selectorsA string containing one or more selectors to match. This string must be a validSélecteurs CSSString

Technical details

Return value:Represents the document with the specifiedSélecteurs CSSThe Element object of the first element matched by the collection, if no match is found, then return null.
Exceptions:SyntaxError-La syntaxe de la chaîne de sélecteur spécifiée est invalide
Version DOM :Niveau DOM1

Plus d'exemples

Obtenez le premier élément avec id="para" :

document.querySelector('#para');
Vérifiez et voyez‹/›

Obtenez le premier élément avec class="demo" :

document.querySelector(".demo");
Vérifiez et voyez‹/›

Obtenez le premier élément <p> du parent <div> dans le document :

document.querySelector("div > p");
Vérifiez et voyez‹/›

Références associées

Tutoriel CSS :Sélecteurs CSS

Référence CSS :CSS#idSélecteurs

Référence CSS :CSS .classSélecteurs

DOM Document querySelectorAll()Méthodes

DOM Document getElementsByClassName()Méthodes

DOM Document getElementsByTagName()Méthodes

Objet Document HTML DOM