javascript - JQuery droppable() 无法识别 "this"对象引用

标签 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 = 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);

};

关于javascript - JQuery droppable() 无法识别 "this"对象引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10121406/

相关文章:

javascript - 如何使用纯 HTML JS 和 CSS 创建这种(见下图)效果?

javascript - 如何使用ajax将数据库值发布到下一页

javascript - 在 ajax 回调 jquery 中使用 $(this)

javascript - JQuery水平居中的div

javascript对象访问性能

javascript - Facebook 使用 GWT 打开图表

javascript - 通过 React Native 中的辅助函数发送状态更新

ruby - 在 rails 中创建新对象

Javascript 时间戳转 php - mysql 时间戳类型

python - 在 statsmodels 结果中使用 result.prsquared() 调用拟合优度值 TypeError : 'numpy.float64' object is not callable