源代码:
下载代码
AI 编程工具
点击运行
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> <style> .container { background-color: tomato; color: white; padding: 20px; } </style> </head> <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>
运行结果: