Javascript 为什么文本区域的高度不能改变
有人知道为什么会这样吗? 当我评论Javascript 为什么文本区域的高度不能改变,javascript,height,Javascript,Height,有人知道为什么会这样吗? 当我评论 // $(this).css("height",height1); 当我删除或添加行时,height1显示textarea的正确高度,但当我取消注释该行时,警报不正确,即使我删除一行,高度也只会越来越高 谢谢 我想在添加或删除行时更改此文本区域的高度 <textarea style="width:380px;height:auto" name="MeStatusDes" id="MeStatusDes" ></textarea>
// $(this).css("height",height1);
当我删除或添加行时,height1显示textarea的正确高度,但当我取消注释该行时,警报不正确,即使我删除一行,高度也只会越来越高
谢谢
我想在添加或删除行时更改此文本区域的高度
<textarea style="width:380px;height:auto" name="MeStatusDes" id="MeStatusDes" ></textarea>
<script>
$("#MeStatusDes").keyup(function(e){
height1 = this.scrollHeight + "px";
alert(height1);
$(this).css("height",height1); // when I uncomment this, all alert is correct
});
</script>
您的代码不起作用,因为当您设置新高度时,您也会增加scrollHeight。 但是,当您删除行时,它不会以这种方式工作,因为浏览器首先解析函数,然后调整textarea的大小 我只是放了一些控制台日志以便更好地解释。我所做的就是按回车键,然后退格 $MeStatusDes.keyupfunction{ height1=此值。滚动高度+px; console.log'scrollHeight:'+height1; console.log“当前高度:”+$this.csheight; $this.c高度,高度1; console.log'newheight:'+$this.csheight; }; 按Enter键时的控制台输出 按下退格键时的控制台输出
以下是您可能需要的信息:在lat,我发现它可以工作,即使我不知道它的细节
var minRows = 5;
var maxRows = 26;
function ResizeTextarea(id) {
var t = document.getElementById(id);
if (t.scrollTop == 0) t.scrollTop=1;
while (t.scrollTop == 0) {
if (t.rows > minRows)
t.rows--; else
break;
t.scrollTop = 1;
if (t.rows < maxRows)
t.style.overflowY = "hidden";
if (t.scrollTop > 0) {
t.rows++;
break;
}
}
while(t.scrollTop > 0) {
if (t.rows < maxRows) {
t.rows++;
if (t.scrollTop == 0) t.scrollTop=1;
} else {
t.style.overflowY = "auto";
break;
}
}
}
scrollHeight: 40px
current height: 36px
new height: 40px
var minRows = 5;
var maxRows = 26;
function ResizeTextarea(id) {
var t = document.getElementById(id);
if (t.scrollTop == 0) t.scrollTop=1;
while (t.scrollTop == 0) {
if (t.rows > minRows)
t.rows--; else
break;
t.scrollTop = 1;
if (t.rows < maxRows)
t.style.overflowY = "hidden";
if (t.scrollTop > 0) {
t.rows++;
break;
}
}
while(t.scrollTop > 0) {
if (t.rows < maxRows) {
t.rows++;
if (t.scrollTop == 0) t.scrollTop=1;
} else {
t.style.overflowY = "auto";
break;
}
}
}
var minRows = 5;
var maxRows = 26;
function ResizeTextarea(id){
var t = document.getElementById(id);
if (t.scrollTop == 0) t.scrollTop=1;
while (t.scrollTop == 0){
if (t.rows > minRows)
t.rows--;
else
break;
t.scrollTop = 1;
if (t.rows < maxRows)
t.style.overflowY = "hidden";
if (t.scrollTop > 0){
t.rows++;
break;
}
}
while(t.scrollTop > 0){
if (t.rows < maxRows){
t.rows++;
if (t.scrollTop == 0) t.scrollTop=1;
}
else{
t.style.overflowY = "auto";
break;
}
}
}