jquery textarea 根据内容自动撑开高度

 
<textarea name="" autoHeight="true" id="remarks" style="width: 100%;border: none;resize: none;" placeholder="内容呈现"></textarea>
 
//监听浏览器宽度的改变
window.onresize = function(){
    changeMargin();
};
 
function changeMargin(){
    $.fn.autoHeight = function(){    
        this.each(function(){
            console.log(this);
            autoHeight(this);
            $(this).on('keyup', function(){
                autoHeight(this);
            });
        });   
        function autoHeight(elem){
            elem.style.height = 'auto';
            elem.scrollTop = 0; //防抖动
            elem.style.height = elem.scrollHeight + 'px';
        }
    }                
    $('textarea[autoHeight]').autoHeight();
}
changeMargin(); //初始化
posted @ 2021-07-20 08:30  芳香四溢713051  阅读(406)  评论(0编辑  收藏  举报