<textarea id="suggest" type="text" name="suggest" class="form-control" style="width:100%;height:50px!important" onkeydown="textareaHightAuto(this.id)"></textarea>
function textareaHightAuto(ids) {
    var id = $("#" + ids)[0]//获取当前id所有元素
    var setStyle = function (id) {
        id.style.height = 'auto';
        id.style.height = id.scrollHeight + 'px';
    }
    var delayedResize = function (id) {
        window.setTimeout(function () {
            setStyle(id)
        }, 0);
    }
    if (id.addEventListener) {
        id.addEventListener('input', function () {
            setStyle(id)
        }, false);
        setStyle(id)
    } else if (id.attachEvent) {
        id.attachEvent('onpropertychange', function () {
            setStyle(id)
        });
        setStyle(id)
    }
    
    if (window.VBArray && window.addEventListener) { //IE9
        id.attachEvent("onkeydown", function () {
            var key = window.event.keyCode;
            if (key == 8 || key == 46) delayedResize(ids);
        });
        id.attachEvent("oncut", function () {
            delayedResize(id);
        }); //处理粘贴
    }
}

 

posted on 2019-01-29 13:25  博客园197  阅读(224)  评论(0编辑  收藏  举报