textarea的高度自适应

匿名 (未验证) 提交于 2019-12-02 22:56:40

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!