首页 > 解决方案 > 使用 jQuery 自动扩展输入(输入文本)

问题描述

如何使输入类型自动扩展?该代码适用于 textarea 但不适用于输入类型文本框。

$('input').on({
  input: function() {
    var totalHeight = $(this).prop('scrollHeight') - parseInt($(this).css('padding-top')) - parseInt($(this).css('padding-bottom'));
    $(this).css({
      'height': totalHeight
    });
  }
});
input {
  width: 200px;
  height: 15px;
  line-height: 15px;
  min-width: 200px;
  max-width: 300px;
  transition: width 0.25s;
  resize: none;
  overflow: hidden;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="myinput" runat="server" placeholder="Text" type="text" data-autosize-input='{ "space": 40 }' />

标签: javascriptjqueryhtml

解决方案


推荐阅读