2016-01-09 44 views
0

我發現下面的代碼用於調整textarea的大小以根據內容進行擴展。在頁面加載時調整textarea的大小

$(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"); 
 
    }); 
 
});

代碼工作很好,但只有文字區域選擇它們並作出更改後延伸。

我希望他們延長頁面加載已經....任何想法?

謝謝!

回答

1

試試這個:

$(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"); 
    } 
}); 
+0

非常感謝你...你的代碼使textarea的對負載擴大,但改變內容時不會展開。如果我使用這兩個函數(你和我的例子),它的工作原理非常好。但是,是否應該有一種方法可以同時執行兩個操作? – sasos

+0

@sasos不客氣,我將代碼示例更新爲您要求的內容,然後嘗試一下。 :) –

+0

像Facebook一樣真棒。 :) 再次感謝! – sasos

相關問題