根据其中有多少文本来扩展文本区域(jquery)

发布于 2024-12-18 18:42:16 字数 57 浏览 3 评论 0原文

如何让文本区域最初只有 1 行,但当文本到达行末尾时添加另一行,并继续执行此操作,直到最多 5 行?

How can I get a textarea to initially have only 1 row, but then when the text reaches the end of the row to add another row, and keep doing this up until a maximum of 5 rows?

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

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

发布评论

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

评论(5

旧竹 2024-12-25 18:42:16

像这样的东西:

<textarea id="foo" rows="1" cols="40"></textarea>

$(function () {

    $("#foo").on("keyup", function () {

       if ($(this).attr('rows') < 5 &&
           parseInt($(this).val().length/$(this).attr('cols')) >= $(this).attr('rows'))
           $(this).attr("rows", parseInt($(this).attr("rows"))+1);

    });

});

http://jsfiddle.net/Q3pPT/

编辑 轻微改进处理换行符:

$(function () {

    $("#foo").on("keyup", function (e) {

       if ($(this).attr('rows') < 5)
       {
           if (
               parseInt($(this).val().length/$(this).attr('cols'))+
               ($(this).val().split("\n").length-1) 

               >= 

               $(this).attr('rows')
              )
               $(this).attr("rows", parseInt($(this).attr("rows"))+1);

           if (e.keyCode == 13)
           {
               $(this).attr("rows", parseInt($(this).attr("rows"))+1);
           }
       } 

    });

});

http://jsfiddle.net/Q3pPT/2/

Something like this:

<textarea id="foo" rows="1" cols="40"></textarea>

$(function () {

    $("#foo").on("keyup", function () {

       if ($(this).attr('rows') < 5 &&
           parseInt($(this).val().length/$(this).attr('cols')) >= $(this).attr('rows'))
           $(this).attr("rows", parseInt($(this).attr("rows"))+1);

    });

});

http://jsfiddle.net/Q3pPT/

edit Slight improvements to handle newlines:

$(function () {

    $("#foo").on("keyup", function (e) {

       if ($(this).attr('rows') < 5)
       {
           if (
               parseInt($(this).val().length/$(this).attr('cols'))+
               ($(this).val().split("\n").length-1) 

               >= 

               $(this).attr('rows')
              )
               $(this).attr("rows", parseInt($(this).attr("rows"))+1);

           if (e.keyCode == 13)
           {
               $(this).attr("rows", parseInt($(this).attr("rows"))+1);
           }
       } 

    });

});

http://jsfiddle.net/Q3pPT/2/

檐上三寸雪 2024-12-25 18:42:16

这篇列表部分文章提供了深入的步骤有关如何最好地实现此功能的分步说明。

This A List Part article gives an in-depth, step-by-step description on how to best implement this functionality.

孤星 2024-12-25 18:42:16

我从 Jake Feasel 的答案中得到了这个想法,它不仅适用于扩展,还适用于收缩文本区域,以防内容较少。

HTML

<textarea id="foo" rows="1" cols="40"></textarea>
<div id="stat"></div>

它还根据单独 div 中的内容显示所需的理想行数

CSS

​#foo
{
resize:none;        
}​

这是必需的,因为一旦使用此处理程序调整了文本区域的大小,代码就会停止工作

JS

$(function () {

    $("#foo").on("keyup", function (e) {       
        var idealRows =  parseInt($(this).val().length/$(this).attr('cols'))+
               ($(this).val().split("\n").length-1)+1 ;
        var rows = $(this).attr('rows');

        // for the bugging scroll bar
        if(rows > 4)  $('#foo').css('overflow','auto')
        else $('#foo').css('overflow','hidden')

        // for expanding and contracting         
        if( (idealRows > rows) && (rows < 5) ||  (idealRows < rows) && (rows > 1))
            $(this).attr("rows", idealRows);                    

    });

});​

演示: http://jsfiddle.net/roopunk/xPdaP/3/

请注意,它还处理在 之间弹出的滚动条keyDown 和 keyUp。 IMO:这有点烦人。

笔记
希望这有帮助!:)

I took the idea from Jake Feasel's answer and it not only works for expanding but also for contracting the textarea in case there is lesser content.

HTML

<textarea id="foo" rows="1" cols="40"></textarea>
<div id="stat"></div>

It also displays the ideal number of rows required according to the content in a sepatate div

CSS

​#foo
{
resize:none;        
}​

This is required because, once the textarea has been resized using this handler, the code stops working

JS

$(function () {

    $("#foo").on("keyup", function (e) {       
        var idealRows =  parseInt($(this).val().length/$(this).attr('cols'))+
               ($(this).val().split("\n").length-1)+1 ;
        var rows = $(this).attr('rows');

        // for the bugging scroll bar
        if(rows > 4)  $('#foo').css('overflow','auto')
        else $('#foo').css('overflow','hidden')

        // for expanding and contracting         
        if( (idealRows > rows) && (rows < 5) ||  (idealRows < rows) && (rows > 1))
            $(this).attr("rows", idealRows);                    

    });

});​

Demo: http://jsfiddle.net/roopunk/xPdaP/3/

Notice that it also takes care of the scroll bar which pops up between keyDown and keyUp. IMO: It is kinda bugging.

Note
Hope this helps!:)

千仐 2024-12-25 18:42:16

检查 Elastic jQuery 插件: http://unwrongest.com/projects/elastic/

您可以使用CSS 上的 max-height 属性与文本区域的 maxlength 相结合,将其限制为 5 行。

Check the Elastic jQuery plugin: http://unwrongest.com/projects/elastic/

You can use the max-height property on your CSS combined with the maxlength of your textarea to limit it to 5 lines.

好听的两个字的网名 2024-12-25 18:42:16

下载此插件: http://james.padolsey.com/demos/plugins /jQuery/autoresize.jquery.js

$('textarea#comment').autoResize({
    // On resize:
    onResize : function() {
        $(this).css({opacity:0.8});
    },
    // After resize:
    animateCallback : function() {
        $(this).css({opacity:1});
    },
    // Quite slow animation:
    animateDuration : 300,
    // More extra space:
    extraSpace : 40
});

download this plugin : http://james.padolsey.com/demos/plugins/jQuery/autoresize.jquery.js

$('textarea#comment').autoResize({
    // On resize:
    onResize : function() {
        $(this).css({opacity:0.8});
    },
    // After resize:
    animateCallback : function() {
        $(this).css({opacity:1});
    },
    // Quite slow animation:
    animateDuration : 300,
    // More extra space:
    extraSpace : 40
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文