代码示例:(标识:jsck_element_matches_1)
<!DOCTYPE html>
<html>
<style>
.container {
  background-color: tomato;
  color: white;
  padding: 20px;
}
</style>

<body>
<h1>Element 对象</h1>

<h2>matches() 方法</h2>

<p id="demo" class="container"></p>

<script>
const element = document.getElementById("demo");
let text;
if (element.matches(".container")) {
  text = "该元素匹配 CSS 选择器";
} else {
  text = "该元素不匹配 CSS 选择器";
}
document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>
运行结果: