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 附加到更多,这有点难看,为什么不给 div 一个“更多”类

  <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 回答