我在嘗試爲textarea設置scrollTop值時遇到了問題。我的JavaScript代碼如下 -如何設置textarea的scrollTop值?
var element = document.getElementById("messageTextArea");
console.log("scrollTop = "+element.scrollTop);
console.log("scrollHeight = "+element.scrollHeight);
element.scrollTop = element.scrollHeight; // doesn't work!
console.log("The value is-->"+element.scrollTop); // no change!
element = document.getElementById("messageTextArea");
console.log("Now scrollTop = "+element.scrollTop); // no change!
console.log("Now scrollHeight = "+element.scrollHeight);
Firefox的控制檯日誌提供了以下 -
scrollTop = 0
scrollHeight = 86
The value is-->0
Now scrollTop = 0
Now scrollHeight = 86
我真正想要做的是使textarea的莫名其妙自動向下滾動到最大時,文字不符合實際的寬度和高度,滾動條被激活。
下面的兩個截圖說明問題 -
這是我目前 -
這是我想擁有的東西 -
請幫忙!
你試過['.scrollIntoView'](https://developer.mozilla.org/ EN/DOM/element.scrollIntoView)? – 2012-03-08 21:31:32
@GGG我試過element.scrollIntoView(false),但它仍然沒有工作。 :( – CodeBlue 2012-03-08 21:36:48
它應該工作,如果你將它稱爲一個元素,你追加到聊天記錄的底部。 – 2012-03-08 21:44:03