Sun_Blue_Sky

菩提本无树,明镜亦非台,本来无一物,何处惹尘埃 寻求内心的平静
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

动态改变并显示文本域的高度

Posted on 2010-02-21 17:18  Sun_Blue_Sky  阅读(212)  评论(0)    收藏  举报
代码
// 设置textarea的高度
function SetAutoHeight(obj)
{
    
// 取原始高度
    if (obj.getAttribute("oldHeight"0== null)
    {
        obj.setAttribute(
"oldHeight", obj.style.pixelHeight, 1);
    }
    
*/
    oldHeight
=obj.getAttribute("Height"0);
    
    
if (obj.scrollHeight>oldHeight)
    {
        obj.style.pixelHeight
=obj.scrollHeight+4;
    }
    
else
    {
        obj.style.pixelHeight
=oldHeight;
    }
}

<asp:textbox id="txtMemo" runat="server" Width="635px" onpropertychange="SetAutoHeight(this);"
                TextMode
="MultiLine" Height="38px" Rows="4"></asp:textbox>(2000字节)


然后加载时:
<script language="javascript" type="text/javascript">
var objArea
=document.getElementById("txtMemo");
if(objArea.scrollHeight > 38)
{
    objArea.style.posHeight
=objArea.scrollHeight;
}
</script>