谢了一个js类,但是在使用过程中出现了点问题,属性访问不了,请大家帮我看一下

function Drag(nodename){
this.node = document.getElementById(nodename);
this.node.style.position = "absolute";
console.log(this.node); //输出为div1...
addEventHandler(this.node, "mousedown", this.start);
console.log(this.node); //输出为div1...
}

        Drag.prototype.start = function(event){
                        console.log(this.node);  [color=red]//输出为undefined???[/color]
          /*  this._startPosition = {
                left: this.node.offsetLeft,
                top: this.node.offsetTop
            };*/
            addEventHandler(this.node, "mousemove", this.move);
            addEventHandler(this.node, "mouseup", this.stop);

        };
        Drag.prototype.move = function(event){
            if (this.node._drag) 
                clearInterval(this.node._drag);
            var nodeStyle = this.node.style;
            var posArr = ["left", "top"];
            var position = [{
                left: event.clientX,
                top: event.clientY
            }];
            this.node._drag = setInterval(function(){
                for (var i = 0; i < posArr.length; i++) {
                    if (Math.round(this._startPosition[posArr[i]]) == position[posArr[i]]) {
                        for (var i = 0; i < posArr.length; i++) {
                            nodeStyle[posArr[i]] = position[posArr[i]] + "px";
                        }
                        clearInterval(node.move);
                    }
                    else {
                        this._startPosition[posArr[i]] += (position[posArr[i]] - this._startPosition[posArr[i]]) * speed;
                        nodeStyle[posArr[i]] = this._startPosition[posArr[i]] + "px";
                    }
                }
            }, 50);
        };
        Drag.prototype.stop = function(event){
            removeEventHandler(this.node, "mousemove", this.move);
        };

        var addEventHandler = function(target, eventType, func){

            console.log(target);  //输出为div1...
            if (target.addEventListener) {
                target.addEventListener(eventType, func, false);
                console.log("out add");
                                    console.log(target);  //输出为div1...
            }
            else 
                if (target.attachEvent) {
                    target.attachEvent("on" + eventType, func);
                }
                else {
                    target[eventType] = func;
                }

            console.log(target);  //输出为div1...
        };
        var removeEventHandler = function(target, eventType, func){
            if (target.removeEventListener) {
                target.removeEventListener(eventType, func, false);
                console.log("out remove");
            }
            else 
                if (target.detachEvent) {
                    target.detachEvent("on" + eventType, func);
                }
                else {
                    target[eventType] = null;
                }
        }; 

标注红色的地方访问不到属性,请大家帮忙,谢谢。

2个回答

this 有问题

我也没细看,你调试看看this指的是那个对象不就行了。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问