HTML DOM matches() method
Example
Does the element match the CSS selector?
const
element
=
document
.
getElementById
(
"
demo
"
)
;
let
answer
=
element
.
matches
(
"
.container
"
)
;
Try it »
Example
Does the element match one of two CSS selectors?
const
element
=
document
.
getElementById
(
"
demo
"
)
;
let
answer
=
element
.
matches
(
"
.container, .wrapper
"
)
;
Try it »
Definition and Usage
If the element matches the specified CSS selector, the matches() method returns true, otherwise returns false.
Browser support
The numbers in the table are the first browser versions that fully support the matches() method:
| Chrome | ||||
| 33 | Edge | |||
| 15 | Firefox | |||
| 34 | Safari | |||
| 7 | Opera | |||
| 21 | ||||
| Feb 2014 | Apr 2017 | Dec 2014 | Oct 2013 | May 2014 |
Syntax
element.matches(selectors)
Parameters
| Parameters |
| selectors |
要匹配的一个或多个 (多个用逗号 , 分隔) CSS 选择器。
返回的元素是在 HTML 文档中找到的第一个元素。
查看所有 [ CSS 选择器参考手册](/cssref/css-selectors.html)。 |
Return Value
| Type | Description |
|---|---|
| Boolean |
true - The element matches the CSS selector. false - The element does not match the CSS selector. |
Element object