ParentNode.querySelector() - Web APIs 编辑
Draft
This page is not complete.
The ParentNode
mixin defines the querySelector()
method as returning an Element
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()
instead.
Note: This method is implemented as Document.querySelector()
, DocumentFragment.querySelector()
and Element.querySelector()
.
Syntax
element = parentNode.querySelector(selectors);
Parameters
selectors
- A
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, aSyntaxError
exception is thrown. See Locating DOM elements using selectors for more information about using selectors to identify elements. Multiple selectors may be specified by separating them using commas.
Note: 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 Escaping special characters for more information.
Return value
The first Element
that matches at least one of the specified selectors or null
if no such element is found.
Note: If the specified selectors
include a CSS pseudo-element, the returned value is always null
.
Exceptions
SyntaxError
- The syntax of the specified
selectors
string is not valid.
Specifications
Specification | Status | Comment |
---|---|---|
DOM The definition of 'ParentNode.querySelector()' in that specification. | Living Standard | Living standard |
Selectors API Level 2 The definition of 'ParentNode.querySelector()' in that specification. | Obsolete | No change |
DOM4 The definition of 'ParentNode.querySelector()' in that specification. | Obsolete | Initial definition |
Selectors API Level 1 The definition of 'document.querySelector()' in that specification. | Obsolete | Original definition |
Browser compatibility
BCD tables only load in the browser
See also
- Locating DOM elements using selectors
- Code snippets for
querySelector()
- Attribute selectors in the CSS Guide
- Attribute selectors in the MDN Learning Area
- This method is available as
Element.querySelector()
,Document.querySelector()
, andDocumentFragment.querySelector()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论