开发者

jQuery - get reference to this

function Request(params)
{
    // Stuff stuff stuff

    // And then

    $.ajax(
    {
        type: 'GET',
        url: 'so开发者_开发百科meurl',
        success: this.done
    });
}

Request.prototype.done = function()
{
    // "this" in this context will not refer to the Request instance.
    // How to reach it?
}


You could capture "this" first:

function Request(params)
{
    // Stuff stuff stuff

    // And then

    var $this = this;

    $.ajax(
    {
        type: 'GET',
        url: 'someurl',
        success: function() { $this.done(); }
    });
}


Apparently you can add the "context" parameter to the ajax request, like so:

$.ajax(
{
    type: 'GET',
    url: 'someurl',
    success: this.done,
    context: this
});


this is not reffering to the same thing!!!


try following:

function Request(params)
{
   var that = this;

....

Request.prototype.done = function()
{
   that...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜