【发布时间】:2011-08-28 12:34:06
【问题描述】:
考虑以下示例:(Live demo here)
HTML:
<textarea></textarea>
<div id="button">Click Here</div>
CSS:
textarea {
height: 80px;
width: 150px;
background-color: #bbb;
resize: none;
border: none;
}
#button {
width: 150px;
background-color: #333;
color: #eee;
text-align: center;
}
JS:
$(function() {
var textarea = $("textarea");
textarea.val("Hello\nStack\nOverflow!\nThere\nAre\nLots\nOf\nGreat\nPeople\nHere");
$("#button").click(function() {
textarea.val(textarea.val() + "!");
});
textarea.scrollTop(9999).focus(); // Arbitrary big enough number to scroll to the bottom
});
当点击#button 时,textarea 的值会发生变化,并且由于某种原因滚动条会移到顶部(我在 Firefox 中检查过,不确定其他浏览器)。
为什么会这样?
我该如何解决这个问题?
我找到了here 一种可能的解决方案,但我想知道是否还有其他解决方案。
【问题讨论】:
-
请注意,Chrome 不会发生这种情况。
标签: javascript jquery firefox scroll textarea