<input type=txt value='你好'  onpropertychange="this.width=(this.value.length==0?2:this.value.length)*20"/>

解决方案 »

  1.   

    用代码来给你解答<script>
    /* 单个汉字的宽度,根据你的字体大小自行设定 */
    var wordWidth = '14';
    function setWidth()
    {
    obj = event.srcElement;
    obj.style.width = obj.value.replace(/[^\x00-\xff]/g,"**").length*wordWidth/2+5;
    }
    </script>
    <input id="i1" onkeypress="setWidth()" onblur="setWidth()" type="text" />
      

  2.   

    用代码来给你解答参考了1楼的<script>
    /* 单个汉字的宽度,根据你的字体大小自行设定 */
    var wordWidth = '14';
    function setWidth()
    {
    obj = event.srcElement;
    obj.style.width = obj.value.replace(/[^\x00-\xff]/g,"**").length*wordWidth/2+5;
    }
    </script>
    <input id="i1" onpropertychange="setWidth()" type="text" />