2011-12-29 26 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'); 
相关问题