gpt4 book ai didi

jQuery $(this) 与 $.post() 的问题

转载 作者:行者123 更新时间:2023-11-28 04:01:43 24 4
gpt4 key购买 nike

下面是当用户点击关注按钮时我的代码:

$('.follow_btn').click(function() {
$(this).html('<img src = "../assets/style_images/loading.gif">');
var userId = $(this).attr('id');
$.post('../assets/scripts/ajax_follow_parse.php', {
userId: userId
}, function(data) {
$(this).html(data);
});
});

很高兴用第2行所示的加载gif替换它。但是当它返回数据并用第4行返回的数据替换它时。

我该如何解决这个问题?

最佳答案

$(this) 赋给 $.post() 之外的变量:

var $this = $(this);

然后,不使用 $(this) 添加数据,而是使用我们刚刚创建的变量:

$this.html(data);

再次查看您的代码,您也可以这样做:

$("#" + userId).html(data);

因为您已经有了元素的 id

关于jQuery $(this) 与 $.post() 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8684669/

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