源代码:
下载代码
AI 编程工具
点击运行
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> <style> .example { border: 1px solid black; margin: 5px; } </style> </head> <body> <div class="example"> 使用 class="example" 的 div </div> <div class="example"> 另外一个使用 class="example" 的 div </div> <p class="example">使用 class="example" 的 p 元素</p> <p>这是一个使用了 class="example" 的 <span class="example">span</span> 元素,它在 p 元素里面。</p> <p>点击按钮修改所有 class="example" 元素的背景颜色。</p> <button onclick="myFunction()">点我</button> <p><strong>注意:</strong>Internet Explorer 8 及更早版本不支持 querySelectorAll() 方法。</p> <p id="demo"></p> <script> function myFunction() { var x = document.querySelectorAll(".example"); var i; for (i = 0; i < x.length; i++) { x[i].style.backgroundColor = "red"; } } </script> </body> </html>
运行结果: