gpt4 book ai didi

javascript - 为什么当我放入此行时我的 jQuery 出现错误?

转载 作者:行者123 更新时间:2023-11-28 12:49:00 25 4
gpt4 key购买 nike

为什么当我放入此行时我的 jQuery 出现错误?

 <script type="text/javascript">
$(function(){
$(".arrowbutton").click(function(){
id = $(this).attr('rel');
$.ajax({
type:"POST",
url:"/upvote",
data:{'id':id},
beforeSend:function() {
},
success:function(html){
$(this).hide();
}
});
return false;
});

});
</script>

当我删除 $(this).hide(); 时就很好了。但是,我希望它隐藏起来!!!

最佳答案

因为 this 不是指您的箭头按钮,而是指 AJAX 请求对象。

$(".arrowbutton").click(function(){
var that = this;
var id = $(this).attr('rel');
$.ajax({
type:"POST",
url:"/upvote",
data:{'id':id},
beforeSend:function() {

},
success:function(html){
$(that).hide();
}
});

return false;
});

jQuery 或多或少像这样调用您的 success 函数:

handleSuccess: function( s, xhr, status, data ) {
// If a local callback was specified, fire it and pass it the data
if ( s.success ) {
s.success.call( s.context, data, status, xhr );
}

// Fire the global callback
if ( s.global ) {
jQuery.triggerGlobal( s, "ajaxSuccess", [xhr, s] );
}
},

call方法的第一个参数将success函数中的this关键字设置为s.context

关于javascript - 为什么当我放入此行时我的 jQuery 出现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4058126/

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