老师帮我看一下哪里有问题
问题描述:
图片没有显示出来,另外我对绑定监听函数为什么要备份上下文不太明白,还请老师讲解
相关代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>红绿灯小案例</title>
<style>
#box img {
width: 80px;
}
</style>
</head>
<body>
<div id="box"></div>
<script>
function TrafficLight() {
this.color = 1;
this.init();
this.bindEvent();
}
TrafficLight.prototype.init = function () {
this.dom = document.createElement('img');
this.dom.scr = 'images/' + this.color + '.jpg';
box.appendChild(this.dom);
};
TrafficLight.prototype.bindEvent = function () {
var self = this;
this.dom.onclick = function () {
self.changeColor();
};
};
TrafficLight.prototype.changeColor = function () {
this.color++;
if (this.color == 4) {
this.color = 1;
}
this.dom.src = 'images/' + this.color + '.jpg';
};
var box = document.getElementById('box');
var count = 10;
while (count--) {
new TrafficLight();
}
</script>
</body>
</html>
14
收起
正在回答 回答被采纳积分+1
2回答
相似问题
登录后可查看更多问答,登录/注册



恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星