草案
此页面不完整。

ParentNode mixin defines the querySelector() method as returning an 元素 representing the first element matching the specified group of selectors which are descendants of the object on which the method was called.

If you need all the elements matching the selector list, use querySelectorAll() 代替。

注意: This method is implemented as Document.querySelector() , DocumentFragment.querySelector() and Element.querySelector() .

句法

element = parentNode.querySelector(selectors);
					

参数

selectors
DOMString containing one or more selectors to match against. This string must be a valid compound selector list supported by the browser; if it's not, a SyntaxError exception is thrown. See 使用选择器定位 DOM 元素 for more information about using selectors to identify elements. Multiple selectors may be specified by separating them using commas.

注意: Characters which are not part of standard CSS syntax must be escaped using a backslash character. Since JavaScript also uses backspace escaping, special care must be taken when writing string literals using these characters. See 转义特殊字符 了解更多信息。

返回值

The first 元素 that matches at least one of the specified selectors or null if no such element is found.

注意: 若指定 selectors include a CSS pseudo-element , the returned value is always null .

异常

SyntaxError
The syntax of the specified selectors string is not valid.

规范

规范 状态 注释
DOM
The definition of 'ParentNode.querySelector()' in that specification.
实时标准 Living standard
Selectors API Level 2
The definition of 'ParentNode.querySelector()' in that specification.
过时 无变化
DOM4
The definition of 'ParentNode.querySelector()' in that specification.
过时 初始定义
Selectors API Level 1
在该规范中的 document.querySelector() 定义。
过时 Original definition

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
querySelector Chrome 1 Edge 12 Firefox 3.5 IE 9
9
Internet Explorer 8 only supported CSS2 selectors.
Opera 10 Safari 4 WebView Android ≤37 Chrome Android 18 Firefox Android Yes Opera Android 10.1 Safari iOS 3.2 Samsung Internet Android Yes

图例

完整支持

完整支持

见实现注意事项。

另请参阅

元数据

  • 最后修改: