gpt4 book ai didi

jquery - 有没有办法让 JQuery ajax 成功函数访问它所包含的对象?

转载 作者:行者123 更新时间:2023-12-01 00:04:35 24 4
gpt4 key购买 nike

我有这样的 JavaScript:

function Cat() { 

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

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

}

var TopCat = new Cat();

这不起作用,因为“this”在成功函数的上下文中没有意义。有没有优雅的解决方案?

最佳答案

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

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

关于jquery - 有没有办法让 JQuery ajax 成功函数访问它所包含的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5378622/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com