在扩展功能中使用magic e

时间:2012-07-27 08:42:01

标签: javascript javascript-events

我有一个基本的Drop事件处理程序,如下所示:

function Drop(e){
    e.preventDefault();
    var data=e.dataTransfer.getData("Text");
    var child = $('[target_id="'+data+'"]').get(0);
    e.target.appendChild(child);
}
function DropHandler(){}
DropHandler.prototype={
        ...
    Drop : Drop
}

创建一个新对象并扩展基本的drop handler:

function AdminDropHandler(){}
//Extend the DropHandler
extend(AdminDropHandler,DropHandler);
//Override Drop method in AdminDropHandler
AdminDropHandler.prototype.Drop=function(){
    DropHandler.prototype.Drop.apply(this,arguments);
    var parent_id = e.target.id;// not defined error
    ...
}

它可以扩展DropHandler的功能,但我使用的e似乎丢失了。如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

您尚未在函数中定义e。尝试定义它:

AdminDropHandler.prototype.Drop = function(e) {
    DropHandler.prototype.Drop.apply(this, arguments);
    var parent_id = e.target.id; // should work now
};

您也可以这样做,但不建议这样做,因为正式参数可以完成这项工作 很好:

AdminDropHandler.prototype.Drop = function() {
    var e = arguments[0];
    DropHandler.prototype.Drop.apply(this, arguments);
    var parent_id = e.target.id; // should work now
};