Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. Element
  4. matches()

Element: matches() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨April 2017⁩.

Thematches() method of theElement interface tests whether the element would be selected by the specifiedCSS selector.

Syntax

js
matches(selectors)

Parameters

selectors

A string containing validCSS selectors to test theElement against.

Return value

true if theElement matches theselectors. Otherwise,false.

Exceptions

SyntaxErrorDOMException

Thrown ifselectors cannot be parsed as a CSS selector list.

Examples

HTML

html
<ul>  <li>Orange-winged parrot</li>  <li>Philippine eagle</li>  <li>Great white pelican</li></ul>

JavaScript

js
const birds = document.querySelectorAll("li");for (const bird of birds) {  if (bird.matches(".endangered")) {    console.log(`The ${bird.textContent} is endangered!`);  }}

This will log "The Philippine eagle is endangered!" to the console, since the elementhas indeed aclass attribute with valueendangered.

Specifications

Specification
DOM
# ref-for-dom-element-matches①

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp