2011-12-29 35 views
3

我想在人們更改內容時獲得<textarea>的滾動高度。問題是當我添加內容時,我可以獲得越來越多的滾動高度,但是當我刪除內容時,我可以「T這裏得到降低scrollheight.And是代碼刪除內容時無法獲得textarea的滾動高度

$("textarea[id^='_sub_ialready_']").live("keydown keyup focus blur", 
    function() 
    { 
     if(aobj.minHeight == 0) 
     aobj.minHeight = $(this).height(); 
     if($(this).attr("scrollHeight") > aobj.minHeight) 
     { 
      if($(this).attr("scrollHeight") > aobj.maxHeight) 
      { 
       nheight = aobj.maxHeight; 
       $(this).css("overflow-Y",'scroll');  
      }  
      else 
      { 
       nheight = $(this).attr("scrollHeight"); 
       $(this).css("overflow-Y",'hidden');  
      } 
      $(this).height(nheight); 
     } 
    } 
) 
+0

你可以指定你的CSS或一點點更多的代碼,所以我們可以更好地理解你想要做什麼?此外,你有沒有檢查出有關獲取scrollHeight的這些問題?: http://stackoverflow.com/questions/5980150/jquery-js-get-the-scrollbar-height-of-an-textarea http:/ /stackoverflow.com/questions/642353/dynamically-scrolling-a-textarea http://stackoverflow.com/questions/454202/creating-a-textarea-with-auto-resize – elboletaire 2011-12-30 02:31:20

回答

0

怎樣......

.attr('scrollHeight') 

OR

$('textarea:first').get(0).scrollHeight 

OR(返回像素的整數)

textarea.scrollHeight 
+1

問題是:當我添加內容我可以獲得越來越高的滾動高度,但是當我刪除內容時,我無法獲得縮小的滾動高度 – ivyandrich 2012-01-04 03:10:54

2

你想要做的是設置高度auto第一,像這樣:

$('#textarea').height('auto'); 

然後獲得scrollHeight屬性屬性:

var h = $('#textarea').prop('scrollHeight'); 
相關問題