Jquery$(这个)不起作用,但$(';选择器';)起作用
因此,我发送一个ajax请求,如下所示Jquery$(这个)不起作用,但$(';选择器';)起作用,jquery,this,selector,Jquery,This,Selector,因此,我发送一个ajax请求,如下所示 $('a.new_thumb_update').click(function(e) { e.preventDefault(); share_id = $(this).attr('share_id'); $.post('http://studeez.net/resource_layout_files/includes/thumbs_up.php', {share_id:share_id}, function(data
$('a.new_thumb_update').click(function(e)
{
e.preventDefault();
share_id = $(this).attr('share_id');
$.post('http://studeez.net/resource_layout_files/includes/thumbs_up.php',
{share_id:share_id},
function(data)
{
alert(data);
$(this).parent().parent().find('div.thumbs_up').hide();
});
});
代码的功能很好,直到回调函数,我在那里得到警报反馈
$(this).parent().parent().find('div.thumbs_up').hide();
如果我将$(this)替换为实际的元素,它就会工作。如下图所示
$('a.new_thumb_update').parent().parent().find('div.thumbs_up').hide();
试试这个
$('a.new_thumb_update').click(function(e){
e.preventDefault();
share_id = $(this).attr('share_id');
var self=this;// assign this to self and use it
$.post('http://studeez.net/resource_layout_files/includes/thumbs_up.php',
{share_id:share_id},
function(data){
alert(data);
$(self).parent().parent().find('div.thumbs_up').hide();// use self instead of this
});
});
试试这个
$('a.new_thumb_update').click(function(e){
e.preventDefault();
share_id = $(this).attr('share_id');
var self=this;// assign this to self and use it
$.post('http://studeez.net/resource_layout_files/includes/thumbs_up.php',
{share_id:share_id},
function(data){
alert(data);
$(self).parent().parent().find('div.thumbs_up').hide();// use self instead of this
});
});
您需要在此处缓存$(this)
:
$('a.new_thumb_update').click(function (e) {
e.preventDefault();
var $self = $(this);
share_id = $self.attr('share_id');
$.post('http://studeez.net/resource_layout_files/includes/thumbs_up.php', {
share_id: share_id
}, function (data) {
alert(data);
$self.parent().parent().find('div.thumbs_up').hide();
});
});
as$(此)
在jQuery ajax$中不可访问。post
方法。您需要在此处缓存$(此)
:
$('a.new_thumb_update').click(function (e) {
e.preventDefault();
var $self = $(this);
share_id = $self.attr('share_id');
$.post('http://studeez.net/resource_layout_files/includes/thumbs_up.php', {
share_id: share_id
}, function (data) {
alert(data);
$self.parent().parent().find('div.thumbs_up').hide();
});
});
as
$(this)
在jQuery ajax$.post
方法中不可访问。在那里,$(this)
不起作用的原因是您输入了另一个函数$(此)
现在指的是您调用的对象$。post
在上,这不是$(此)
在那里不起作用的原因,是因为您输入了另一个函数$(此)
现在指的是您调用的对象$。post
on,这不是什么!我真不敢相信这段小小的代码让我在午餐时间忙得不可开交。啊!我不敢相信这段小小的代码让我在午餐时间忙得不可开交。