直接上程式碼
<textarea id="textarea1" onkeyup="AutoExpand(this)" onfocus="AutoExpand(this)">
複製程式碼
<script type="text/javascript">
var InitHeight = 20;
function AutoExpand(e) {
var HideTextarea = document.getElementById("__hidetextarea");
if (!HideTextarea) {
HideTextarea = document.createElement("textarea");
HideTextarea.id = "__hidetextarea";
HideTextarea.style.borderWidth = 0;
document.body.appendChild(HideTextarea);
}
HideTextarea.value = e.value;
HideTextarea.style.height = "0px";
HideTextarea.style.overflow = "hidden";
var x = HideTextarea.scrollHeight + "必須先計算下scrollHeight的值,否則,scrollHeight 的值將固定?";
delete x;
e.style.height = Math.max(Math.max(InitHeight,20), HideTextarea.scrollHeight) + "px";
}
</script>
複製程式碼
結語
以上是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援。