JQuery ajax成功函数是否可以访问其中包含的对象? [英] Is there a way for a JQuery ajax success function to access the object it's contained in?

查看:79
本文介绍了JQuery ajax成功函数是否可以访问其中包含的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的javascript:

I have javascript like this:

function Cat() { 

  this.meow = function() { // meow };

  $.ajax( do AJAX call, success: this.meow(); );

}

var TopCat = new Cat();

这不起作用,因为在成功功能的上下文中"this"没有意义.有一个优雅的解决方案吗?

This does not work because 'this' makes no sense in the context of the success function. Is there an elegant solution?

推荐答案

您正在寻找ajax方法的context参数.
它允许您设置将在其中调用所有回调的上下文.

You're looking for the context parameter to the ajax method.
It allows you to set the context in which all callbacks will be called.

function Cat() { 
    this.meow = function() { // meow };
    $.ajax({
        context: this, 
        success: function() { this.meow(); } 
    });    
}

这篇关于JQuery ajax成功函数是否可以访问其中包含的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆