gpt4 book ai didi

javascript - 在扩展函数中使用 magic e

转载 作者:行者123 更新时间:2023-12-02 19:29:28 25 4
gpt4 key购买 nike

我有一个基本的 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
}

创建一个新对象并扩展基本的放置处理程序:

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似乎丢失了。如何解决这个问题?

最佳答案

您还没有在函数中的任何地方定义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
};

关于javascript - 在扩展函数中使用 magic e,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11684348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com