DocumentFragment.querySelector()
method returns the first element, or
null
if no matches are found, within the
DocumentFragment
(using depth-first pre-order traversal of the document's nodes) that matches the specified group of selectors.
If the selector matches an ID and this ID is erroneously used several times in the document, it returns the first matching element.
If the selectors specified in parameter are invalid a
DOMException
采用
SYNTAX_ERR
value is raised.
element = documentfragment.querySelector(selectors);
DOMString
containing one or more CSS selectors separated by commas.
In this basic example, the first element in the
DocumentFragment
with the class "
myclass
" is returned:
var el = documentfragment.querySelector(".myclass");
The string argument pass to
querySelector
must follow the CSS syntax. To match ID or selectors that do not follow the CSS syntax (by using semicolon or space inappropriately for example), it's mandatory to escape the wrong character with a double back slash:
<div id="foo\bar"></div>
<div id="foo:bar"></div>
<script>
document.querySelector('#foo\bar') // Does not match anything
document.querySelector('#foo\\\\bar') // Match the first div
document.querySelector('#foo:bar') // Does not match anything
document.querySelector('#foo\\:bar') // Match the second div
</script>
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
querySelector
|
Chrome 1 | Edge 12 | Firefox 3.5 | IE 9 | Opera 10 | Safari 3.2 | WebView Android Yes | Chrome Android Yes | Firefox Android 4 | Opera Android 10.1 | Safari iOS 3.2 | Samsung Internet Android Yes |
完整支持
DocumentFragment
interface it belongs to.
DocumentFragment
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
DocumentType
元素
ElementTraversal
Entity
EntityReference
事件
EventTarget
HTMLCollection
MutationObserver
节点
NodeFilter
NodeIterator
NodeList
NonDocumentTypeChildNode
ProcessingInstruction
PromiseResolver
范围
StaticRange
文本
TextDecoder
TextEncoder
TimeRanges
TreeWalker
TypeInfo
USVString
UserDataHandler
XMLDocument