Element.querySelector() - Web APIs 编辑
The querySelector()
method of the Element
interface returns the first element that is a descendant of the element on which it is invoked that matches the specified group of selectors.
Syntax
element = baseElement.querySelector(selectors);
Parameters
selectors
- A group of selectors to match the descendant elements of the
Element
baseElement
against; this must be valid CSS syntax, or aSyntaxError
exception will occur. The first element found which matches this group of selectors is returned.
Return value
The first descendant element of baseElement
which matches the specified group of selectors
. The entire hierarchy of elements is considered when matching, including those outside the set of elements including baseElement
and its descendants; in other words, selectors
is first applied to the whole document, not the baseElement
, to generate an initial list of potential elements. The resulting elements are then examined to see if they are descendants of baseElement
. The first match of those remaining elements is returned by the querySelector()
method.
If no matches are found, the returned value is null
.
Exceptions
SyntaxError
- The specified
selectors
are invalid.
Examples
Let's consider a few examples.
Find a specific element with specific values of an attribute
In this first example, the first <style>
element which either has no type or has type "text/css" in the HTML document body is returned:
var el = document.body.querySelector("style[type='text/css'], style:not([type])");
The entire hierarchy counts
This example demonstrates that the hierarchy of the entire document is considered when applying selectors
, so that levels outside the specified baseElement
are still considered when locating matches.
HTML
<div>
<h5>Original content</h5>
<p>
inside paragraph
<span>inside span</span>
inside paragraph
</p>
</div>
<div>
<h5>Output</h5>
<div id="output"></div>
</div>
JavaScript
var baseElement = document.querySelector("p");
document.getElementById("output").innerHTML =
(baseElement.querySelector("div span").innerHTML);
Result
The result looks like this:
Notice how the "div span"
selector still successfully matches the <span>
element, even though the baseElement
's child nodes do not include the div
element (it is still part of the specified selector).
More examples
See Document.querySelector()
for additional examples of the proper format for the selectors
.
Specifications
Specification | Status | Comment |
---|---|---|
DOM The definition of 'querySelector()' in that specification. | Living Standard | |
Selectors API Level 2 The definition of 'querySelectorAll()' in that specification. | Obsolete | |
Selectors API Level 1 The definition of 'querySelectorAll()' in that specification. | Obsolete |
Browser compatibility
BCD tables only load in the browser
See also
- Locating DOM elements using selectors
- Attribute selectors in the CSS Guide
- Attribute selectors in the MDN Learning Area
Element.querySelectorAll()
Document.querySelector()
andDocument.querySelectorAll()
DocumentFragment.querySelector()
andDocumentFragment.querySelectorAll()
ParentNode.querySelector()
andParentNode.querySelectorAll()
- Code snippets for querySelector
- Other methods that take selectors:
element.closest()
andelement.matches()
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论