JQ - input宽度自适应

input宽度自适应输入的内容长度:

HTML:

  <input class="write" type="text" style="width: 50px">

JQ:

    $(function(){
        //propertychange监听input里面的字符变化,属性改变事件
        $('body').on('input propertychange','.write', function() {
            var $this = $(this);
            console.log($this);
            var text_length = $this.val().length;//获取当前文本框的长度
            var current_width = parseInt(text_length) *16;//该16是改变前的宽度除以当前字符串的长度,算出每个字符的长度
            console.log(current_width);
            $this.css("width",current_width+"px");
        });
    })

猜你喜欢

转载自blog.csdn.net/lm1022/article/details/79386489
jq