0

我有一个ul包含许多链接。当使用 jQuery 加载这些链接时,它会同时呈现它们。尝试了以下操作后,如何一一加载它们:

js:

$('#publish').click(function() {
    $('#get_links li').
        each(function() {
            $(this).load('test.php',{'post':$(this).text()});
            $(this).html("<i class='icon-spin icon-spinner'></i>Loading");

            ///NEED TOSLEEP UNTILL $.load finish..
        }
        );
     }
);

我怎样才能让 .each 循环直到 .load 完成?

4

1 回答 1

2

您可以$().load为此使用回调:

$('#publish').click(function(){
    var $link = $('#get_links li').first();

    var loadLink = function() {
        $link.load('test.php', { post: $link.text() }, function() {
            // once loading is done, grab the next link...
            $link = $link.next();
            // ... and load it if we found one
            if ($link.length) loadLink();
        }).html('<i class="icon-spin icon-spinner"></i>Loading');
    };

    // load the first link
    loadLink();
});
于 2013-04-07T21:08:09.160 回答