开发者社区 问答 正文

input如何根据文本自适应宽度

<input class="form-control" type="text" placeholder="input如何根据文本自适应长度" />

展开
收起
小旋风柴进 2016-05-27 11:13:43 3503 分享 版权
1 条回答
写回答
取消 提交回答
  • <html>
    <head>
    <script type="text/javascript" src="/jquery/jquery.js"></script>
    <script>
    function demo() {
    var testLength = document.getElementById('test').value.length
    $("#test").css('width', testLength*5 + 'px')
    }
    </script>
    <body>
    <input id="test" onkeypress="demo()" type="text"/>
    </body>
    </html>
    2019-07-17 19:17:26
    赞同 展开评论
问答地址: