创建具有自动调整大小的文本区域
有另一个主题,我已经尝试过了.但是有一个问题:如果删除内容,textarea
不会缩小.我找不到任何方法将其缩小到正确的大小 - clientHeight
值返回为 textarea
的完整大小,而不是其内容.
There was another thread about this, which I've tried. But there is one problem: the textarea
doesn't shrink if you delete the content. I can't find any way to shrink it to the correct size - the clientHeight
value comes back as the full size of the textarea
, not its contents.
该页面的代码如下:
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 )
};
}
这对我有用(Firefox 3.6/4.0 和 Chrome 10/11):
This works for me (Firefox 3.6/4.0 and Chrome 10/11):
var observe;
if (window.attachEvent) {
observe = function (element, event, handler) {
element.attachEvent('on'+event, handler);
};
}
else {
observe = function (element, event, handler) {
element.addEventListener(event, handler, false);
};
}
function init () {
var text = document.getElementById('text');
function resize () {
text.style.height = 'auto';
text.style.height = text.scrollHeight+'px';
}
/* 0-timeout to get the already changed text */
function delayedResize () {
window.setTimeout(resize, 0);
}
observe(text, 'change', resize);
observe(text, 'cut', delayedResize);
observe(text, 'paste', delayedResize);
observe(text, 'drop', delayedResize);
observe(text, 'keydown', delayedResize);
text.focus();
text.select();
resize();
}
textarea {
border: 0 none white;
overflow: hidden;
padding: 0;
outline: none;
background-color: #D0D0D0;
}
<body onload="init();">
<textarea rows="1" style="height:1em;" id="text"></textarea>
</body>
如果你想试试 jsfiddle它从一条线开始,只增长所需的确切数量.对于单个 textarea
来说是可以的,但我想写一些东西,在那里我会有很多很多这样的 textarea
(大约和通常在一个大文本文档).在这种情况下,它真的很慢.(在 Firefox 中它非常慢.)所以我真的想要一种使用纯 CSS 的方法.contenteditable
可以做到这一点,但我希望它是纯文本的.
If you want try it on jsfiddle
It starts with a single line and grows only the exact amount necessary. It is ok for a single textarea
, but I wanted to write something where I would have many many many such textarea
s (about as much as one would normally have lines in a large text document). In that case it is really slow. (In Firefox it's insanely slow.) So I really would like an approach that uses pure CSS. This would be possible with contenteditable
, but I want it to be plaintext-only.