我发现以下代码用于调整文本区域的大小以根据内容进行扩展。



$(document).ready(function() {
  $(".content").on("keyup",function(){
    var h = $(this).height();
    $(this).css("height","0px");
    var sh = $(this).prop("scrollHeight");
    var minh = $(this).css("min-height").replace("px", "");
    $(this).css("height",Math.max(sh,minh)+"px");
  });
});





该代码运行良好,但是文本区域仅在选择并进行更改后才能扩展。

我希望它们已经扩展到页面加载中。...有什么想法吗?

谢谢!

最佳答案

尝试这个:

$(document).ready(function() {
    var $content = $('.content');
    $.each($content, resize);
    $content.on("keyup", resize);

    function resize(){
        var h = $(this).height();
        $(this).css("height","0px");
        var sh = $(this).prop("scrollHeight");
        var minh = $(this).css("min-height").replace("px", "");
        $(this).css("height",Math.max(sh,minh)+"px");
    }
});

09-25 16:48
查看更多