删除内容时无法获取textarea的滚动高度

发布于 2024-12-23 10:51:30 字数 797 浏览 2 评论 0原文

我想在人们更改内容时获取

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

I want to get the scrollheight of <textarea> when people changing the content.The problem is when I add the content ,I can get a growing scrollheight ,but when I delete contents,I can't get a Reduced scrollheight。And here is the code

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娜些时光,永不杰束 2024-12-30 10:51:30

您要做的就是首先将高度设置为 auto,如下所示:

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

然后获取scrollHeight 属性:

var h = $('#textarea').prop('scrollHeight');

What you want to do is set height to auto first, like so:

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

Then get the scrollHeight property:

var h = $('#textarea').prop('scrollHeight');
梦回旧景 2024-12-30 10:51:30

怎么样...

.attr('scrollHeight')

OR

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

OR(返回以像素为单位的整数)

textarea.scrollHeight

What about...

.attr('scrollHeight')

OR

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

OR (returns an integer in pixels)

textarea.scrollHeight
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文