我想做以下事情......
$('.showcomments').click(function()
{
$(this).parent().hide();
jQuery.getJSON('comments.json',function($data)
{
$(this).parent().append($data['value'])
//this is meant to be the instance of
//$('.showcomments') that has been clicked
});
});
问题是getJSON的回调当然没有继承这个项目......但是我该如何做我想要的呢?
答案 0 :(得分:7)
在变量中引用它:
$('.showcomments').click(function()
{
var $th = $(this); // References the clicked .showcomments
$th.parent().hide();
jQuery.getJSON('comments.json',function($data)
{
$th.parent().append($data['value']); // will reference the correct element
});
});