textarea 高度自動

<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);
        }); //處理粘貼
    }
}
相關文章
相關標籤/搜索