源代码:
下载代码
AI 编程工具
点击运行
<!DOCTYPE html> <html> <body> <p>点击按钮修改 body 元素所有子元素的背景颜色。</p> <button onclick="myFunction()">点我</button> <h2>我是 h2 元素</h2> <div>我是 div 元素</div> <span>我是 span 元素</span> <script> function myFunction() { var c = document.body.children; var i; for (i = 0; i < c.length; i++) { c[i].style.backgroundColor = "red"; } } </script> </body> </html>
运行结果: