input宽度随文字变化而变化

<input type="text" class="form-control">
<script>
var searchinput=$("input");
var textWidth = function (text) {
            var sensor = $('
' + text + '
'
).css({display: 'none'}); $('body').append(sensor); var width = sensor.width(); sensor.remove(); return width; }; searchinput.unbind('keyup').bind('keyup', function () { $(this).width(textWidth($(this).val() + 20)); });
script>

你可能感兴趣的:(javascript)