現時点では、ユーザーが div をクリックしたときにコメントを読み込むために、次の jQuery/Ajax を使用しています。
function showComments(post_id) {
$("#comment-"+post_id).toggle();
$.ajax({
dataType: 'html',
cache: false,
url: 'ajax.php',
type: 'POST',
data: {comments: post_id}
}).done(function(html) {
$('#comment-'+post_id).html(html)
});
}
デフォルトの div にはスピナーがあり、コンテンツが読み込まれるまで表示されるため、これはすべて正常に機能します。ただし、ユーザーがもう一度クリックして非表示にした場合、コンテンツが明らかにリセットされていないため、スピナーをクリックして再度表示すると、スピナーは表示されません。だから私は .toggles マウスイベントハンドラーを使用しようとしました:
function showComments(post_id) {
$("#comment-"+post_id).toggle(function() {
$('#comment-'+post_id).show();
$.ajax({
dataType: 'html',
cache: false,
url: 'ajax.php',
type: 'POST',
data: {comments: post_id}
}).done(function(html) {
$('#comment-'+post_id).html(html)
});
}, function() {
$('#comment-'+post_id).hide();
$('#comment-'+post_id).html('<img src="/loader.gif" />')
});
}
ただし、今回は何も機能せず、div が表示または非表示にならず、Ajax が呼び出されません。私がおそらく間違ったことをしたアイデアはありますか?
編集:
HTML/PHP は次のとおりです。
<div class="comments right" title="Click to view comments" onclick="showComments('.$post['id'].')">'.$post['comments_cache'].'</div>
<div style="display:none" id="comment-'.$post['id'].'" class="top-15"><img src="/loader.gif" /></div>
上記に問題はありません。最初に示した関数で問題なく動作します。