0

10 個のエントリを表示する最後に [さらに表示] ボタンを使用して、小さなスクリプトを作成しています。これがコードです

<div id="more<?=$lastid;?>">
   <a onclick="showmore(<?=$lastid;?>);">More</a>
</div>  

そしてスクリプトは

function showmore(lastid) {

    $.post("/ajax/showmore.php", {
        lastid: lastid,

    }, function(response) {
        $("#more" + lastid).show();
        setTimeout("finishAjax('more' + lastid, '" + escape(response) + "')", 300);
    });

    return false;
}

function finishAjax(a, b) {
    $("#" + a).html(unescape(b));
    $("#" + a).fadeIn(1e3)
}​

しかし、スクリプトは動作しませんか? ここで何が問題なのですか?

一定の div id を使用してスクリプトをテストしたところ、正常に動作しましたが、div に $lastid を追加し、スクリプト側に $("#more" + lastid) を追加すると機能しませんでした。

DIV IDを変更可能にするアイデアはありますか?

前もって感謝します

4

3 に答える 3

1

id を more に追加する代わりに、div に "more" クラスを指定しないでください。

  <div id="<?=$lastid;?>" class="more">
       <a onclick="showmore(<?=$lastid;?>);" >More</a>
  </div>  

jqueryセレクターは次のようになります。

 $("div.more#" + id).show()
于 2012-06-01T00:45:42.293 に答える
1

setTimeout() の文字列内で lastid を参照します。この問題を解決するには、クロージャーを使用する必要があります。ただし、ここにクリーンバージョンがあります:

function showmore(lastid) {
    $.post("/ajax/showmore.php",
        { lastid:lastid },
        resultHandler);

    return false;

    function resultHandler(response) {
        $("#more" + lastid).html(response).fadeIn(1e3);
    }
}
于 2012-06-01T00:50:02.883 に答える
0

皆さん、ありがとうございました

エラーが見つかりました

この列にありました

            setTimeout("finishAjax('more' + lastid, '"+escape(response)+"')", 300);

に訂正します

            setTimeout("finishAjax('more"+lastid+"', '"+escape(response)+"')", 300);

回答ありがとうございます

于 2012-06-01T00:54:38.683 に答える