微商城页面设计/电脑系统优化工具
<!-- 点击按钮将8个div盒子都设置为红色:
1. 获取button和div对象
2. button上绑定点击onclick事件
3. 使用for循环来得到所有的div,将他们背景设置为红色:
for(var i = 0;i < odiv.length; i++){odiv[i].style.backgroundColor = "red";}
4. 顺序我要试试看--><!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.Box {width: 100px;height: 100px;border: 1px solid black;}</style><script>window.onload = function () {var oredBtn = document.getElementById("redBtn");var oBox = document.getElementsByClassName("Box");oredBtn.onclick = function () {for (var i = 0; i < oBox.length; i++) {oBox[i].style.backgroundColor = "red";}}// for (var i = 0; i < oBox.length; i++) {// oredBtn.onclick = function () {// oBox[i].style.backgroundColor = "red";// }// }}</script>
</head><body><button id="redBtn">按钮</button><div class="Box"></div><div class="Box"></div><div class="Box"></div><div class="Box"></div><div class="Box"></div><div class="Box"></div><div class="Box"></div><div class="Box"></div>
</body></html>