<!-- style -->
.d1 {
width: 100px;
height: 100px;
background-color: aqua;
}
<div class="d1"></div>
<div class="d1"></div>
//script
<script>
var d1 = document.getElementsByClassName('d1')
// 通过call继承实现forEach遍历
Array.prototype.forEach.call(d1, function (item, index) {
d1[index].addEventListener('click', function () {
if (d1[index].style.backgroundColor == 'aqua') {
this.style.backgroundColor = 'red'
} else {
this.style.backgroundColor = 'aqua'
}
})
})
//Array 数组构造器-》 一个数组是他的实例
// 数组的方法在 __proto__
// 构造器的prototype 属性的对象里携带的
var Array = function () {
}
</script>
效果如图所示,成功实现