源代码:
下载代码
AI 编程工具
点击运行
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <h1>HTMLCollection item() 方法</h1> <p>第一个 p 元素</p> <p class="myclass">第二个 p 元素</p> <p class="myclass">第三个 p 元素</p> <p>点击按钮修改所有 class="myclass" 元素的内容并添加背景颜色:</p> <button onclick="myFunction()">点我</button> <script> function myFunction() { var x, i; x = document.getElementsByClassName("myclass"); for (i = 0; i < x.length; i++) { x.item(i).style.backgroundColor = "red"; } } </script> </body> </html>
运行结果: