【发布时间】:2016-06-04 11:47:18
【问题描述】:
我在这个 jsfiddle http://jsfiddle.net/tovic/gLhCk/ 中找到了以下非常简单的方法来自动将 textarea 调整为文本高度:
function expandTextarea(id) {
document.getElementById(id).addEventListener('keyup', function() {
this.style.overflow = 'hidden';
this.style.height = 0;
this.style.height = this.scrollHeight + 'px';
}, false);
}
expandTextarea('txtarea');
body {
padding:50px;
}
textarea {
margin:0px 0px;
padding:5px;
height:16px;
line-height:16px;
width:96%;
display:block;
margin:0px auto;
}
<textarea id="txtarea" spellcheck="false" placeholder="Statusku..."></textarea>
但是,我在我的项目中使用引导程序,这会带来一些问题。
- textarea 仅 4px 高而不是 16px。似乎引导程序改变了边距、填充和高度的工作方式?
- 回车时,文字上下跳动,刺眼。
我尝试在浏览器的 HTML 检查器中删除所有引导类,但问题仍然存在。有谁知道如何解决这个问题?
下面是添加 bootstrap 的 CSS 时的代码:
function expandTextarea(id) {
document.getElementById(id).addEventListener('keyup', function() {
this.style.overflow = 'hidden';
this.style.height = 0;
this.style.height = this.scrollHeight + 'px';
}, false);
}
expandTextarea('txtarea');
body {
padding:50px;
}
textarea {
margin:0px 0px;
padding:5px;
height:16px;
line-height:16px;
width:96%;
display:block;
margin:0px auto;
}
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<textarea id="txtarea" spellcheck="false" placeholder="Statusku..."></textarea>
【问题讨论】:
-
我也一直在寻找如何自动扩展 textarea。我正在做一个 python 教程并使用 pycharm。我把所有这些东西放在哪里? >.> 到目前为止我的代码是:
标签: css twitter-bootstrap textarea