自动滚动

发布于 2024-09-19 01:52:40 字数 641 浏览 2 评论 0原文

我有一个显示输入文本的文本区域,但是当行数超过文本区域的大小和宽度时,用户必须向下滚动才能查看他们上次输入的内容。

我希望每次按下回车按钮时将文本区域设置为底部。

我已经尝试了以下方法,但无法让它工作:

function inputKeyDown(evt, input) {    
    if (evt.keyCode == 13) {    
        var textarea = document.getElementById("textarea");  
        textarea.value += "\n" + ">" + " " + input.value;  
        input.value = "";  
        return false;       
    }  
    var elem = document.getElementById('textarea');  
    elem.scrollTop = elem.scrollHeight;    
} 

然后我在 知道

为什么没有workie吗?

I have this textarea that shows inputted text, but when the number of lines exceeds the size and width of the textarea, the user has to scroll down to see what they have last inputted.

I'd like the textarea to be set to the bottom everytime the enter button is pressed.

I've tried the following, but I can't get it to work:

function inputKeyDown(evt, input) {    
    if (evt.keyCode == 13) {    
        var textarea = document.getElementById("textarea");  
        textarea.value += "\n" + ">" + " " + input.value;  
        input.value = "";  
        return false;       
    }  
    var elem = document.getElementById('textarea');  
    elem.scrollTop = elem.scrollHeight;    
} 

and then I call the function keyDown in <input onKeyDown="return keyDown(event, this);" ...>

Any idea why no workie?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月苍老的讽刺 2024-09-26 01:52:40

请尝试以下操作:

textarea.scrollTop = textarea.scrollHeight - textarea.clientHeight;

Try the following:

textarea.scrollTop = textarea.scrollHeight - textarea.clientHeight;
樱娆 2024-09-26 01:52:40

这取决于您正在查看的输入类型,但强制光标返回底部可能会导致严重的可用性问题。如果您需要自动扩展的文本区域,请查看现有的解决方案,例如 jQuery插件

我不太确定这是否是您想要的,但看看这个: http:// jsfiddle.net/yV76p/

var textarea = document.getElementById("textarea");

textarea.onkeyup = function(evt) {
    this.scrollTop = this.scrollHeight;
}

It depends on what sort of input you are looking at, but forcing the cursor back to the bottom could potentially be a serious usability problem. If you need a textarea that automatically expands, have a look at existing solutions such as this jQuery plugin

I'm not really sure if this is what you want but have a look this: http://jsfiddle.net/yV76p/

var textarea = document.getElementById("textarea");

textarea.onkeyup = function(evt) {
    this.scrollTop = this.scrollHeight;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文