无法阅读属性' scrollHeight'为null

时间:2017-10-24 06:32:43

标签: javascript

我收到的错误消息是Cannot read property 'scrollHeight' of null

var b = document.getElementById('chat-messages');
b.scrollTop = b.scrollHeight;

var lst = b.scrollTop;
console.log(lst);

b.addEventListener("scroll", function() {
  if (b.scrollTop > lst) {
    console.log("Greater than Last");
  } else {
    console.log("Less or equal than last.");
  }

  lst = b.scrollTop;
});

请帮帮我。谢谢!

1 个答案:

答案 0 :(得分:0)

jQuery的正确方法是 -

$('#chat-messages')。prop('scrollHeight')或    $( '#聊天消息')[0] .scrollHeight

希望它有所帮助。