【发布时间】:2015-04-24 14:18:55
【问题描述】:
我想在事件中从 HTML 调用下面的代码(右箭头键)。
var Anim = function() {
var box = document.getElementById("square");
};
Anim.prototype.Start = function(event){
if(event.keyCode == 39){
box.style.left = (box.offsetLeft + 100)+"px";
}
};
Anim.prototype.Stop = function(event){
if(event.keyCode == 37){
box.style.left = (box.offsetLeft)+"px";
}
};
var Myanim = new Anim();
这是我的 HTML
<div id="square" style="position: absolute;">This is my box</div>
【问题讨论】:
-
您的
box变量是Anim构造函数的本地变量。您的Start和Stop方法将无法访问它。也许你想use a property?
标签: javascript html oop