Javascript JQuery droppable()无法识别";这";对象引用
我有一个这样的物体:Javascript JQuery droppable()无法识别";这";对象引用,javascript,jquery,object,droppable,Javascript,Jquery,Object,Droppable,我有一个这样的物体: function ImageHolder(Attributes, Parent) { this.Id = Attributes['Id']; this.Parent = Parent; this.Element = $('<div/>'); this.msg = "hello world"; this.Parent.append(this.Element); this.handleDrop = f
function ImageHolder(Attributes, Parent)
{
this.Id = Attributes['Id'];
this.Parent = Parent;
this.Element = $('<div/>');
this.msg = "hello world";
this.Parent.append(this.Element);
this.handleDrop = function(e, ui)
{
alert(this.msg);
};
this.Element.droppable({drop: this.handleDrop});
}
holder = new ImageHolder(A,B);
但是,当我尝试将某些内容放到元素上时,会出现以下错误:
this.msg is undefined
我在这里做错什么了吗?复制一份
var thisCopy = this;
在执行以下功能之前,请像这样替换
this.handleDrop = function(e, ui)
{
alert(thisCopy.msg);
};
此
在handleDrop函数中指的是handleDrop函数本身,而不是其外部的对象。另外,thisCopy
(您可以随意命名)是一个浅拷贝,因此它不会与同一页面上的其他对象冲突,并将同时更新真正的this
。因此,您可以在对象中的任何位置使用此副本。