English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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.
document.querySelector(selectors)
document.querySelector('h1');Vérifiez et voyez‹/›
The numbers in the table specify the first browser version that fully supports the querySelector() method:
Method | |||||
querySelector() | 1 | 3.5 | 10 | 3.2 | 8 |
Parameter | Description |
---|---|
selectors | A string containing one or more selectors to match. This string must be a validSélecteurs CSSString |
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 |
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‹/›
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