在div中向下滚动(在Firefox和IE中!)

时间:2011-12-20 14:45:19

标签: javascript jquery html

<div id="aaa">sdfas fasdfa asdfsdfas fasdfa asdfsdfas fasdfa asdfsdfas fasdfa asdfsdfas fasdfa asdfsdfas fasdfa asdfsdfas fasdfa asdfsdfas fasdfa asdfsdfas fasdfa asdfsdfas fasdfa asdfsdfas fasdfa asdfsdfas fasdfa asdfsdfas fasdfa asdfsdfas fasdfa asdfsdfas fasdfa asdfsdfas fasdfa asdfsdfas fasdfa asdfsdfas fasdfa asdfsdfas fasdfa asdf </div>

#aaa {
height: 100px;
    width: 200px;
    overflow: scroll;
}

$("#aaa").scrollTop = $("#aaa").scrollHeight;  

http://jsfiddle.net/PfA7Q/2/

可以在Firefox和IE中的这个DIV中向下滚动吗?

2 个答案:

答案 0 :(得分:2)

您的代码有点不对,因为jQuery中的scrollTop实际上是一个函数,而不是属性。而scrollHeight是一个原生的javascript属性,与jQuery无关。所以你会想做这样的事情:

$('#aaa').scrollTop($("#aaa")[0].scrollHeight);

http://jsfiddle.net/PfA7Q/14/

话虽这么说,你应该把你的div引用缓存在变量中,而不是两次得到它,如下所示:

var $aaa = $('#aaa');
$aaa.scrollTop($aaa[0].scrollHeight);

答案 1 :(得分:0)

$("#aaa").attr({ scrollTop: $("#aaa").attr("scrollHeight") });

attr替换为prop以获取jQuery 1.6 +。