gpt4 book ai didi

javascript - 如何访问 jQuery 回调函数中的属性?

转载 作者:行者123 更新时间:2023-11-29 16:24:47 25 4
gpt4 key购买 nike

var some_name =
{
moving:false,

show : function ()
{
this.moving = true;

$('element').slideDown(5000, function ()
{
this.moving = false; //How to access to attribute "moving" of class some_name?
});
},
}

代码中的问题。

最佳答案

您可以将回调函数绑定(bind)到当前上下文:

$('element').slideDown(5000, $.proxy(function() {
this.moving = false;
}), this); // "this" inside of the function will be this "this"

参见jQuery.proxy


或者你可以这样做:

this 是当前上下文,它的值取决于函数的调用方式。您可以将 this 分配给函数外部的变量,并改用此变量:

var that = this;
$('element').slideDown(5000, function() {
that.moving = false; //use that instead of this here
});

关于javascript - 如何访问 jQuery 回调函数中的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7221396/

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