textarea的高度自适应

1.只读状态下的高度自适应

//maxHeight为最大高度
function autoTextarea(maxHeight){
        $.each($("textarea"), function(i, n){
            $("#textarea").css("overflow", "hidden");
            var _height = n.scrollHeight;
            $(n).height(_height);
            if( $(n).height() > maxHeight ){
                $(n).height(maxHeight);
                $("#textarea").css("overflow", "auto");
            }
        })
    }

效果图:

 2.编辑状态下的高度自适应

$.each($("textarea"), function(i, n){
 $(n).height(n.scrollHeight);  $(n).on(
'input',function(){ if(n.scrollHeight>n.offsetHeight){  $(n).height(n.scrollHeight)   } })
})

删除时,高度自适应还没有找到合适的方法(待续,如果你们有的话,可以给我留言)

原文地址:https://www.cnblogs.com/zsj-02-14/p/9371011.html