querySelector()
方法在
元素
interface returns the first element that is a descendant of the element on which it is invoked that matches the specified group of selectors.
element = baseElement.querySelector(selectors);
selectors
元素
baseElement
against; this must be valid CSS syntax, or a
SyntaxError
exception will occur. The first element found which matches this group of selectors is returned.
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()
方法。
If no matches are found, the returned value is
null
.
SyntaxError
selectors
are invalid.
Let's consider a few examples.
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])");
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.
<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>
var baseElement = document.querySelector("p");
document.getElementById("output").innerHTML =
(baseElement.querySelector("div span").innerHTML);
结果看起来像这样:
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).
见
Document.querySelector()
for additional examples of the proper format for the
selectors
.
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
querySelector
|
Chrome 1 | Edge 12 | Firefox 3.5 |
IE
9
|
Opera 10 | Safari 3.1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 2 | Samsung Internet Android 1.0 |
完整支持
见实现注意事项。
Element.querySelectorAll()
Document.querySelector()
and
Document.querySelectorAll()
DocumentFragment.querySelector()
and
DocumentFragment.querySelectorAll()
ParentNode.querySelector()
and
ParentNode.querySelectorAll()
element.closest()
and
element.matches()
.
元素
accessKey
属性
childElementCount
children
classList
className
clientHeight
clientLeft
clientTop
clientWidth
currentStyle
firstElementChild
id
innerHTML
lastElementChild
localName
名称
namespaceURI
nextElementSibling
onfullscreenchange
onfullscreenerror
openOrClosedShadowRoot
outerHTML
part
prefix
previousElementSibling
runtimeStyle
scrollHeight
scrollLeft
scrollLeftMax
scrollTop
scrollTopMax
scrollWidth
shadowRoot
slot
tabStop
tagName
after()
animate()
append()
attachShadow()
before()
closest()
computedStyleMap()
createShadowRoot()
getAnimations()
getAttribute()
getAttributeNames()
getAttributeNode()
getAttributeNodeNS()
getAttributeNS()
getBoundingClientRect()
getClientRects()
getElementsByClassName()
getElementsByTagName()
getElementsByTagNameNS()
hasAttribute()
hasAttributeNS()
hasAttributes()
hasPointerCapture()
insertAdjacentElement()
insertAdjacentHTML()
insertAdjacentText()
matches()
msZoomTo()
prepend()
querySelector()
querySelector()
querySelectorAll()
querySelectorAll()
releasePointerCapture()
remove()
removeAttribute()
removeAttributeNode()
removeAttributeNS()
replaceChildren()
replaceWith()
requestFullscreen()
requestPointerLock()
scroll()
scrollBy()
scrollIntoView()
scrollIntoViewIfNeeded()
scrollTo()
setAttribute()
setAttributeNode()
setAttributeNodeNS()
setAttributeNS()
setCapture()
setPointerCapture()
toggleAttribute()
afterscriptexecute
auxclick
blur
click
compositionend
compositionstart
compositionupdate
contextmenu
copy
cut
dblclick
DOMActivate
DOMMouseScroll
error
focus
focusin
focusout
fullscreenchange
fullscreenerror
gesturechange
gestureend
gesturestart
keydown
keypress
keyup
mousedown
mouseenter
mouseleave
mousemove
mouseout
mouseover
mouseup
mousewheel
MozMousePixelScroll
msContentZoom
MSGestureChange
MSGestureEnd
MSGestureHold
MSGestureStart
MSGestureTap
MSInertiaStart
MSManipulationStateChanged
overflow
paste
scroll
select
show
touchcancel
touchend
touchmove
touchstart
underflow
webkitmouseforcechanged
webkitmouseforcedown
webkitmouseforceup
webkitmouseforcewillbegin
wheel
AbortController
AbortSignal
AbstractRange
Attr
ByteString
CDATASection
CSSPrimitiveValue
CSSValue
CSSValueList
CharacterData
ChildNode
注释
CustomEvent
DOMConfiguration
DOMError
DOMErrorHandler
DOMException
DOMImplementation
DOMImplementationList
DOMImplementationRegistry
DOMImplementationSource
DOMLocator
DOMObject
DOMParser
DOMPoint
DOMPointInit
DOMPointReadOnly
DOMRect
DOMString
DOMTimeStamp
DOMTokenList
DOMUserData
Document
DocumentFragment
DocumentType
ElementTraversal
Entity
EntityReference
事件
EventTarget
HTMLCollection
MutationObserver
节点
NodeFilter
NodeIterator
NodeList
NonDocumentTypeChildNode
ProcessingInstruction
PromiseResolver
范围
StaticRange
文本
TextDecoder
TextEncoder
TimeRanges
TreeWalker
TypeInfo
USVString
UserDataHandler
XMLDocument