Javascript 调用原型函数-不工作
您好,我有以下控制图像库位置的代码(可以在steven.tlvweb.com上看到)。滚轮当前可以控制多媒体资料的位置,但按键事件不能控制,我希望它们能够控制。可以看到下面代码中的警报(keyLeft和keyRight),但根本不会调用此.parent.scroll 参数sc只需要是一个正整数或负整数-这就是event.wheelDelta到底是什么,所以我想知道,调用这个原型函数的正确方法是什么Javascript 调用原型函数-不工作,javascript,function,Javascript,Function,您好,我有以下控制图像库位置的代码(可以在steven.tlvweb.com上看到)。滚轮当前可以控制多媒体资料的位置,但按键事件不能控制,我希望它们能够控制。可以看到下面代码中的警报(keyLeft和keyRight),但根本不会调用此.parent.scroll 参数sc只需要是一个正整数或负整数-这就是event.wheelDelta到底是什么,所以我想知道,调用这个原型函数的正确方法是什么 /* //////////// ==== ImageFlow Constructor ==== /
/* //////////// ==== ImageFlow Constructor ==== //////////// */
function ImageFlow(oCont, xmlfile, horizon, size, zoom, border, start, interval) {
this.oc = document.getElementById(oCont);
this.scrollbar = getElementsByClass(this.oc, 'div', 'scrollbar');
this.text = getElementsByClass(this.oc, 'div', 'text');
this.bar = getElementsByClass(this.oc, 'img', 'bar');
this.arL = getElementsByClass(this.oc, 'img', 'arrow-left');
this.arR = getElementsByClass(this.oc, 'img', 'arrow-right');
this.bar.parent = this.oc.parent = this;
this.arL.parent = this.arR.parent = this;
/* === handle mouse scroll wheel === */
this.oc.onmousewheel = function () {
this.parent.scroll(event.wheelDelta);
return false;
}
var pleasework = this;
/* ==== add keydown events ==== */
window.document.onkeydown=function(){
pleasework.keypress(event.keyCode);
return false;
}
}
/* //////////// ==== ImageFlow prototype ==== //////////// */
ImageFlow.prototype = {
scroll: function (sc) {
if (sc < 0) {
if (this.view < this.NF - 1) this.calc(1);
} else {
if (this.view > 0) this.calc(-1);
}
},
keypress : function (kp) {
switch (kp) {
case 39:
//right Key
if (this.view < this.NF - 1) this.calc(1);
break;
case 37:
//left Key
if (this.view > 0) this.calc(-1);
break;
}
},
}
/*////////==ImageFlow构造函数====//////*/
函数ImageFlow(oCont、xmlfile、地平线、大小、缩放、边框、开始、间隔){
this.oc=document.getElementById(oCont);
this.scrollbar=getElementsByClass(this.oc,'div','scrollbar');
this.text=getElementsByClass(this.oc,'div','text');
this.bar=getElementsByClass(this.oc,'img','bar');
this.arL=getElementsByClass(this.oc,'img','arrow left');
this.arR=getElementsByClass(this.oc,'img','arrow right');
this.bar.parent=this.oc.parent=this;
this.arL.parent=this.arR.parent=this;
/*==手柄鼠标滚轮===*/
this.oc.onmouseheel=函数(){
this.parent.scroll(event.wheelDelta);
返回false;
}
var pleasework=此;
/*===添加按键关闭事件===*/
window.document.onkeydown=函数(){
pleasework.keypress(事件.keyCode);
返回false;
}
}
/*////==ImageFlow原型===//////===ImageFlow原型*/
ImageFlow.prototype={
滚动:功能(sc){
if(sc<0){
如果(this.view0)this.calc(-1);
}
},
按键:功能(kp){
开关(kp){
案例39:
//右键
如果(this.view0)this.calc(-1);
打破
}
},
}
提前谢谢
Steven(Java新手程序员)不要在DOM元素上使用
parent
属性。相反,只需在局部变量上创建闭包。那么,替换
this.bar.parent = this.oc.parent = this;
this.arL.parent = this.arR.parent = this;
/* === handle mouse scroll wheel === */
this.oc.onmousewheel = function () {
this.parent.scroll(event.wheelDelta);
return false;
}
/* ==== add keydown events ==== */
window.document.onkeydown=function(){
this.parent.keypress(event.keyCode);
return false;
}
与
我猜你想做
ImageFlow.prototype.scroll=function(sc){
。现在您正在覆盖ImageFlow对象的原型,而不是向其添加函数。您从何处获得parent
?这些oc
和parent
属性是什么?重复的和许多others@Bergi和mVChr-已经在上面的代码中添加了声明。
var parent = this;
this.oc.onmousewheel = function(e) {
parent.scroll(e.wheelDelta);
e.preventDefault();
};
window.document.onkeydown = function(e) { // notice this overwrites previous listeners
parent.keypress(e.keyCode);
};