Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JQuery droppable()无法识别";这";对象引用_Javascript_Jquery_Object_Droppable - Fatal编程技术网

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
。因此,您可以在对象中的任何位置使用此副本。