Summary
Returns the first element within the document (using depth-first pre-order traversal of the document's nodes) that matches the specified group of selectors.
Syntax
element = document.querySelector(selectors );
where
elementis an element object.selectorsis a string containing one or more CSS selectors separated by commas.
Example
In this example, the first element in the document with the ID "myid " is returned:
1 | var el = document.querySelector( "#myid" ); |
Notes
Returns null if no matches are found; otherwise, it returns the first matching element.
Throws a SYNTAX_ERR exception if the specified group of selectors is invalid.
querySelector() was introduced in the Selectors API.
Browser compatibility
- Desktop
- Mobile
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
|---|---|---|---|---|---|
| Basic support | 1 | 3.5 (1.9.1) bug 416317 | 8 | 10 | 3.2 (525.3) WebKit bug 16587 |
本文介绍querySelector方法,该方法采用文档节点的深度优先预序遍历,返回文档中与指定选择器组匹配的第一个元素。若未找到匹配项则返回null,若选择器语法错误则抛出SYNTAX_ERR异常。
1704

被折叠的 条评论
为什么被折叠?



