还有另外一个关于这个问题,我试过了.但是有一个问题:textarea如果删除内容,则不会缩小.我找不到任何方法将它缩小到正确的大小 - clientHeight值返回为完整的大小,而textarea不是其内容.
该页面的代码如下:
function FitToContent(id, maxHeight)
{
var text = id && id.style ? id : document.getElementById(id);
if ( !text )
return;
var adjustedHeight = text.clientHeight;
if ( !maxHeight || maxHeight > adjustedHeight )
{
adjustedHeight = Math.max(text.scrollHeight, adjustedHeight);
if ( maxHeight )
adjustedHeight = Math.min(maxHeight, adjustedHeight);
if ( adjustedHeight > text.clientHeight )
text.style.height = adjustedHeight + "px";
}
}
window.onload = function() {
document.getElementById("ta").onkeyup = function() {
FitToContent( this, 500 ) …Run Code Online (Sandbox Code Playgroud) 如何使用jQuery自动扩展textarea?
我有一个用于解释会议议程的文本框,所以当我的议程文本不断增加该文本框区域时,我想扩展该文本框.
我正在尝试将Ace编辑器添加到页面中,但我不知道如何根据其内容的长度自动设置高度.
理想情况下它会起作用,所以当内容改变时,重新计算高度,但我会对页面加载时设置的高度感到满意.
对于JavaScript新手,有人可以帮我弄清楚我如何计算代码的长度,跨越多少行,新高度是什么以及如何更新DOM以反映这一点?
我在谷歌小组中找到了这个建议,但我真的不明白它在做什么以及如何调整高度.
editor.getSession().getDocument().getLength() *
editor.renderer.lineHeight + editor.renderer.scrollBar.getWidth()
Run Code Online (Sandbox Code Playgroud) 无论如何通过CSS或Javascript根据内容设置textarea的高度?我的CSS中有一个硬编码的高度,但我想要它默认,所以页面加载时没有垂直滚动条?