繁体   English   中英

输入文本时调整输入文本字段的大小

[英]Resize input text field as text is entered

输入文本时是否可以调整文本输入框的大小? 我搜索了 web 并没有找到任何东西。

我刚给你做了一个,在这里试试: http://jsfiddle.net/FNMSP/2/

function autoResize(e){
    var ele=e.target;                          //get the text field
    var t=ele.scrollTop;                       //use scroll top to determine if
    ele.scrollTop=0                              //space is enough
    if(t>0){                                       //If it needs more space....
        ele.style.height=(ele.offsetHeight+t+t)+"px";  //Then add space for it!
    }          
}

您可以对文本区域执行此操作,

<textarea onkeydown="autoResize(event)">Auto Resize!</textarea>

或者使用下面将 function 附加到每个<textarea>

var ele=document.getElementsByTagName("textarea");
for(i=0;i<ele;i++){
    ele[i].addEventListener("keydown",autoResize,false)
}

随意使用它。

非常简单但奇怪的实现:)

<script>
function enlarge(ele)
{
   ele.size += 10;
}
</script>

<form>
<input onkeypress="enlarge(this)" size="10"/>
</form>
<SCRIPT LANGUAGE="JavaScript" type="text/javascript">

  function call_me(max_length) {
      if((document.form1.mybox.value == null ) ||
         (document.form1.mybox.value == "" ))
          document.form1.mybox.size = size;
      if((document.form1.mybox.value.length >= size) &&
         (document.form1.mybox.value.length <= max_length))
          document.form1.mybox.size = document.form1.mybox.value.length + 1;
      else
          document.form1.mybox.size = size;
  }

</script>

LastName: <input type="text" style="font-family: Terminal"
                 name="mybox" maxlength="30" size="10"
                 onFocus="setInterval('call_me(document.form1.mybox.maxLength)', 1)">

<SCRIPT LANGUAGE="JavaScript">

  var size = document.form1.mybox.size; 

</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM