Javascript 自动滚动div不工作

Javascript 自动滚动div不工作,javascript,jquery,css,Javascript,Jquery,Css,我可能想在加载内容时使div自动滚动到页面底部,但实际上它不会滚动jquery上的代码如下: function auto_scrollmes(){ //undefined ang scrollheight. var div = $(".convo_mes").scrollHeight; alert(div); div.scrollTop = div.scrollHeight; } 当我尝试通知div时,它通知未定义。以下是我的css中的内容: #convo_me

我可能想在加载内容时使div自动滚动到页面底部,但实际上它不会滚动jquery上的代码如下:

function auto_scrollmes(){
    //undefined ang scrollheight.
    var div = $(".convo_mes").scrollHeight;
    alert(div);
    div.scrollTop = div.scrollHeight;
}
当我尝试通知div时,它通知未定义。以下是我的css中的内容:

#convo_mes{
text-align:left;
width:98%;
height:80%;
}

.convo_mes{
text-align:left;
width:100%;
height:100%;
background:#fff;
border:1px solid #000;
overflow-x:hidden;
overflow-y:auto;
 }
实际上,当我点击一条消息时,消息的内容将被加载到具有conva_mes类的div上:

$(".mes").click(function(){
    var user = $(this).attr("id");      
    $("#convo").html("<b>"+user+"</b>");
    $("#convo_ctrl").show();
    $(".send_to").attr("id",user);
    $(".convo_mes").html("Loading conversation <img width='15' height='15' src='./img/load.gif'>");

    $.post("./php/view_msg.php",{friend:user},function(view_msg){
        $("#"+user).html(user+" "+view_msg);
    });

    setTimeout(function(){get_convo()},2000);
    setTimeout(function(){auto_scrollmes()},3000);

});
$(“.mes”)。单击(函数(){
var user=$(this.attr(“id”);
$(“#conva”).html(“+user+”);
$(“#conva_ctrl”).show();
$(“.send_to”).attr(“id”,用户);
$(“.conva_mes”).html(“加载对话”);
$.post(“./php/view_msg.php”,{friend:user},函数(view_msg){
$(“#”+user).html(user+“”+view\u msg);
});
setTimeout(函数(){get_conva()},2000);
setTimeout(函数(){auto_scrollmes()},3000);
});
我尝试了一切,以下是我的HTML代码:

<div id="convo_mes">
<div class="convo_mes">

</div>
</div>

我检查了控制台,它显示了“global.js:24 uncaughttypeerror:cannotreadproperty'scrollHeight'of undefined”错误,global.js中的第24行是div.scrollTop=div.scrollHeight

$(“.conva_mes”)
是一个jQuery对象,而不是DOM对象。
像这样访问DOM对象:
$(“.conva_mes”)[0]

这应该行得通

function auto_scrollmes(){
    //undefined ang scrollheight.
    var div = $(".convo_mes")[0];
    alert(div);
    div.scrollTop = div.scrollHeight;
}

现在我看不到控制台日志中的错误,但它仍然不会自动滚动到底部。顺便问一下,DOM和Jquery object之间的区别在于浏览器对文档对象模型的表示。jQuery是一个自定义框架,它包装了DOM,因此您可以在其上运行自定义构建的功能,这也有助于跨浏览器遵从性,因为并非所有浏览器都实现相同的DOM。我看不到您的代码有任何其他问题,没有JSFIDLE,我无能为力。它会提醒1024位用户,但不会滚动到底部(现在我知道问题出在哪里了,哈哈哈。你把代码搞糟了。你写了div.scrollTop,实际上div是scrollHeight的一个变量,所以现在你用它作为div的一个变量,结果你写了$(“.conva_-mes”)[0]。scrollHeight.scrollTop=$(“.conva_-mes”)[0].scrollHeight.scrollHeight这就是它不工作的原因。哈哈哈!在我编辑代码后没有工作。