即时创建一个类似的系统,但我已经遇到了一个问题,当用户按下像“喜欢”的文本应更改为“删除像”jQuery的替换文本内锚
数据来源于一个PHP查询所以那里有与同级别的许多项目
<table class="UserHistoryTable">
<td data-rowtype="stat" data-rowuser="1" data-rowid="1" class="UserHistoryTableTD">
<a href="#" class="like">Like</a></td>
</table>
数据rowuser是用户按下喜欢按钮的ID,数据ROWID是活动 的什么我想要做的是id,当有人按下“像“链接,它应该更改为”删除像“ 我已尝试使用此代码:
$('.like').click(function(){
var the_id = $(this).parent().data('rowid')
var user_id = $(this).parent().data('rowuser')
var user_id = $(this).parent().data('rowtype')
$.ajax({
url: "like.php",
data: "action=likestat" + "&id=" + the_id + "&uid=" + user_id,
success:function(response){
var findtext = $('.UserHistoryTableTD > .like', this);
$(findtext).replaceWith("Remove like");
}
});
});
为什么'$ self'? (只想学习) – samura
@samura - 您必须将'this'的值保存在局部变量中,以便您可以在成功处理程序中使用它(因为'this'将在成功处理程序中具有不同的值)。由于我们需要'$(this)'在几个地方,我保存它而不是'this'。一个受保护的'this'变量的流行约定是'self'。保存jQuery对象的另一个流行约定是以'$'开头。因此,我们最终以'$ self'作为变量名称(一个表示'this'的jQuery对象)。 – jfriend00
哇,这比我问的还要多!感谢您缩短我的代码,并帮助我:)(我喜欢当人们帮助我) – Flaashing