菜单切换
FACESOHO知行者
心灵
记录
远方
赞赏工具
源代码:
点击运行
保存
<!DOCTYPE html> <html> <head> <style> div { border:1px solid black; margin:5px; } </style> </head> <body> <p>点击按钮为 div 的第二个(索引为 1)子元素添加背景颜色。</p> <button onclick="myFunction()">点我</button> <div id="myDIV"> <p>第一个 p 元素</p> <p>第二个 p 元素</p> </div> <script> function myFunction() { var c = document.getElementById("myDIV").children; c[1].style.backgroundColor = "yellow"; } </script> </body> </html>
运行结果