javascript 自动调整大小的文本输入框 html

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19424718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 15:32:46  来源:igfitidea点击:

auto-resizing text input box html

javascriptjqueryhtmlcss

提问by rockerist

I created an input (type text) box and made it auto-resize quite simply. However, there are a few glitches that I can't seem to fix:

我创建了一个输入(输入文本)框并使其自动调整大小非常简单。但是,有一些我似乎无法修复的故障:

  1. when I start typing the box shrinks a tiny bit
  2. when I press backspace (or directional arrows), the box expands first, and then shrinks when I continue typing.
  1. 当我开始打字时,框会缩小一点
  2. 当我按退格键(或方向箭头)时,框首先扩展,然后在我继续输入时缩小。

Here is my code:

这是我的代码:

function Expander() {
     
    this.start = function () {
       
        $("#inputbox").keydown(function(e) {
                  
            this.style.width = 0;
            var newWidth = this.scrollWidth + 10;
       
            if( this.scrollWidth >= this.clientWidth )
                newWidth += 10;
        
            this.style.width = newWidth + 'px';
       
        });
      
    }
     
}
    
    
$(function() {
    window.app = new Expander();
    window.app.start();
});
input {
     margin-left:3em;
     min-width:100px;
     max-width:600px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<meta charset="UTF-8">
<body>

    <div id="wrap">
     <input type="text" id="inputbox" name="input" placeholder="I want this to resize smoothly." width="100px"/>
    </div>
    <div id="counter"></div>
</body>

回答by Peeyush

You can try this.

你可以试试这个。

We put the content in a hidden span& then adjust width according to spanscrollWidth.

我们将内容放在一个隐藏中span,然后根据spanscrollWidth调整宽度。

function Expander() {

    this.start = function () {

        $('#inputbox').on('keydown',function(e) {

            $("#hidden_span").html("");
            $("#hidden_span").append("<p>"+$(this).val()+"</p>");

            var hidden_span_scroll_width=$("#hidden_span")[0].scrollWidth;

            if(hidden_span_scroll_width> 100||hidden_span_scroll_width< 600){
                $(this).css("width",hidden_span_scroll_width);
            }

        });

    }

}


$(function() {
    window.app = new Expander();
    window.app.start();
});
input {
    margin-left:3em;
    min-width:100px;
    max-width:600px;
}

#hidden_span{
    position:absolute;
    top:0px;
    left:0px;
    visibility:hidden;
    width:10px;
    white-space:nowrap;
    overflow:hidden;
}
<head>
<meta charset="UTF-8">
<script src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
</head>

<body>
<div id="wrap">
    <input type="text" id="inputbox" name="input" placeholder="I want this to resize smoothly." />
</div>
<div id="counter"></div>
<span id="hidden_span"></span>
</body>

<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<script src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
<title>Test page</title>
</head>

<body>
<style>
input {
    margin-left:3em;
    min-width:100px;
    max-width:600px;
}
        #hidden_span{
            position:absolute;
            top:0px;
            left:0px;
            visibility:hidden;
            width:10px;
            white-space:nowrap;
            overflow:hidden;

        }
</style>
<script>

function Expander() {

    this.start = function () {

        $('#inputbox').on('keydown',function(e) {


            $("#hidden_span").html("");
            $("#hidden_span").append("<p>"+$(this).val()+"</p>");


            var hidden_span_scroll_width=$("#hidden_span")[0].scrollWidth;

     if(hidden_span_scroll_width> 100||hidden_span_scroll_width< 600){
                $(this).css("width",hidden_span_scroll_width);
                     }


        });

    }

}


$(function() {
    window.app = new Expander();
    window.app.start();
});

</script>
<div id="wrap">
    <input type="text" id="inputbox" name="input" placeholder="yes" />
</div>
<div id="counter"></div>
<span id="hidden_span"></span>
</body>
</html>

回答by wmbtrmb

At first you can use jQuery methods to avoid crossbrowser problem. And scrollWidth returns either the width in pixels of the content of an element or the width of the element itself, whichever is greater. So enlarge newWidth only when content width strongly larger than element width.

首先你可以使用 jQuery 方法来避免跨浏览器问题。scrollWidth 返回元素内容的宽度(以像素为单位)或元素本身的宽度,以较大者为准。所以只有当内容宽度远大于元素宽度时才扩大 newWidth 。

$("#inputbox").keydown(function(e) {

  var a = $(this).width(), newWidth;


  if(this.scrollWidth - a > 0){
    newWidth = a + 10;
  }else if(e.keyCode==8){
    newWidth = a - 10;
  }
  $(this).css("width", newWidth);

});

If you don't want to make input smaller when pressed backspace - delete else if part of code.

如果您不想在按下退格键时使输入变小 - 删除 else if 部分代码。

回答by Joran Den Houting

<input type="text" id="inputbox" name="input" placeholder="yes" width="100px"/>

Remove the width="100px", like this:

删除width="100px",像这样:

<input type="text" id="inputbox" name="input" placeholder="yes"/>

And it won't resize down anymore when starting to type.

并且在开始输入时它不会再缩小。

Live example:

现场示例:

http://jsfiddle.net/2EMfd/

http://jsfiddle.net/2EMfd/

Though, the exanding function doesn't seem to work in my browser?

但是,扩展功能在我的浏览器中似乎不起作用?

*Edit, did a simple expand function on the inputboxis this what you where trying? http://jsfiddle.net/2EMfd/1/

* 编辑,inputbox您在此尝试了一个简单的扩展功能吗?http://jsfiddle.net/2EMfd/1/