文本輸入框自適應高度,文本輸入框自適應
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>文字框textarea高度自適應增長/伸縮</TITLE>
</HEAD>
<BODY>
<textarea id="txtContent1" rows="5" cols="50" onkeyup="ResizeTextarea(1)" style="overflow-y:hidden;resize: none;">Textarea高度隨內容自適應地增長,無捲軸
</textarea>
<script type="text/javascript">
// 最小高度
var minRows = 5;
// 最大高度,超過則出現捲軸
var maxRows = 60;
function ResizeTextarea(index) {
var t = document.getElementById('txtContent' + index);
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;
}
}
}
</script>
</BODY>
</HTML>
自增前:
自增後:
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。