Javascript 能否在$.post()中使用$(this)?
我似乎无法从$.post部分中访问$this。它在外面工作得很好。下面是javascript:Javascript 能否在$.post()中使用$(this)?,javascript,jquery,ajax,dom,Javascript,Jquery,Ajax,Dom,我似乎无法从$.post部分中访问$this。它在外面工作得很好。下面是javascript: $('.idea').each(function(){ var title = $(this).html(); $.post("votes.php", { title: title }, function(data){ $(this).nextAll('.voteTotal').html(data); }, "json")
$('.idea').each(function(){
var title = $(this).html();
$.post("votes.php", { title: title }, function(data){
$(this).nextAll('.voteTotal').html(data);
}, "json");
});
HTML:
您应该在调用回调函数之前备份此函数:
$(".idea").each(function() {
var $this = $(this),
title = $this.html();
$.post("votes.php", { title: title }, function(data) {
$this.nextAll(".voteTotal").html(data);
}, "json");
});
您应该在调用回调函数之前备份此函数:
$(".idea").each(function() {
var $this = $(this),
title = $this.html();
$.post("votes.php", { title: title }, function(data) {
$this.nextAll(".voteTotal").html(data);
}, "json");
});
使用上下文设置,则它将起作用:
$('.idea').each(function(){
var title = $(this).html();
$.post("votes.php", { title: title, context: this }, function(data){
$(this).nextAll('.voteTotal').html(data);
}, "json");
});
使用上下文设置,则它将起作用:
$('.idea').each(function(){
var title = $(this).html();
$.post("votes.php", { title: title, context: this }, function(data){
$(this).nextAll('.voteTotal').html(data);
}, "json");
});
另请参见,您没有在$.post内部使用它,而是在由$.post异步功能调用的匿名函数内部使用它。请参见,您没有在$.post内部使用它,而是在由$.post异步功能调用的匿名函数内部使用它。