0

好的,这就是我想要使用的

$(".shouts").scrollTop = $(".shouts").scrollHeight;

我认为在使用类而不是 id 时这是正确的,但这不起作用,我认为这是因为它上面的代码是

function refreshShouts() {
    var refreshRate = 5000; // time in milliseconds
    var cacheBuster = (new Date).getTime();
    $.ajax({
        type: "POST",
        dataType: "xml",
        url: "php/getShouts.php",
        data: {op: 'tick', time: cacheBuster},
        error: function() { alert('An error occured! Try again.'); },
        success: function(xml){
            if($('shout', xml).size() > 0) {
                $('shout', xml).each(function(id){
                    var nickname = $('nickname', this).text();
                    var message = $('message', this).text();
                    var date = $('date', this).text();
                    var cssClass = 'shoutRow1';
                    if((id%2)/10 == 0)
                        cssClass = 'shoutRow';
                    var shoutRow = '<div id="'+cssClass+'"><span id="nickname">'+nickname+' <span id="date">('+date+')</span>: </span><span id="shoutedMessage">'+message+'</span></div>';
                    if(id == 0)
                        $('#shoutsContainer').empty().removeClass().addClass('shouts').html(shoutRow);
                    else
                        $('#shoutsContainer').removeClass().addClass('shouts').append(shoutRow);
                });
            } else {
                $('#shoutStatus').empty().addClass('shoutError').html($('error', xml).text());
                $('#shoutsContainer').empty().removeClass().addClass('noShouts').html('Shouts will be displayed here!');
            }
            $(".shouts").scrollTop = $(".shouts").scrollHeight;
        }
    });
    setTimeout('refreshShouts()', refreshRate);
}

也许是因为它创建了它,我不确定.. 通常我为此使用 div id 而不是类,但我希望让它工作。

4

1 回答 1

2

It works with IDs because there's only one. But with classes, how does it know which one is which?

Try this:

$(".shouts").each(function() {this.scrollTop = this.scrollHeight;});
于 2012-12-25T02:50:25.753 回答