开发者

Ajax: Appending list-item at the top

I'm calling a php processing file using ajax an appending 开发者_运维百科the returned HTML usign the code below

$.ajax({
    type: "POST",
    url: "proc/process.php",
    data: dataString,
    cache: false,
    success: function(html){
        $("ul#lists").append(html);
        $("ul#lists li:last").fadeIn("slow");
        $("#flash").hide();
    }   
});

It appends a <li></li> item at the end of the ul#lists. I want the returned list-item <li></li> at the top of the lists instead of being appended at the last. How do I do that??


You may try the .prepend() function:

$("ul#lists").prepend(html);
$("ul#lists li:first").fadeIn("slow");

And here's a live demo.


Here is what you want:

$.ajax({
type: "POST",
url: "proc/process.php",
data: dataString,
cache: false,
success: function(retHtml){
    var oldhtml = $("ul#lists").html();
    $("ul#lists").html(retHtml + oldhtml);
    $("ul#lists li:first").fadeIn("slow");
    $("#flash").hide();
}   
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜