如何让textarea更新时保持滚动到底部 [英] How to have a textarea to keep scrolled to the bottom when updated

查看:644
本文介绍了如何让textarea更新时保持滚动到底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个文本区域,有css

code> #object {overflow:hidden; resize:none;} 。我试图阻止它产生滚动条,同时也调整自身。此textarea与外部脚本的控制台同步,意味着它更新。默认情况下,它将保持在顶部,除非你突出显示文本,拖动到元素的底部。这将是唯一的方法向下滚动,而不是用箭头键,自然。



以编程方式,有没有办法在更新时将文本区域保持在底部?

解决方案

你可以通过javascript来实现。使用 scrollHeight 属性设置文本区域的 scrollTop 属性,如下所示:

 document.getElementById(textarea)。scrollTop = document.getElementById(textarea)。scrollHeight 


My problem is a bit off the cuff here, so I'll try to explain this best I can.

I have a text area, having css of #object{overflow:hidden;resize:none;}. I am trying to prevent it from spawning scroll bar while also resizing itself. This textarea is synced with an external script's console meaning it updates. By default however, it will stay at the top unless you highlight text, dragging off to the bottom of the element. This would be the only way to scroll down other than with arrow keys, naturally.

Programmatically, is there any way to keep the text area down to the bottom upon updating? It would be nice to have it auto-scroll to accommodate its use case.

解决方案

You can do it by javascript. Set the scrollTop property of text area with scrollHeight property like below:

document.getElementById("textarea").scrollTop = document.getElementById("textarea").scrollHeight 

这篇关于如何让textarea更新时保持滚动到底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆