Javascript:检测文本区域已满

发布于 2024-11-07 14:55:36 字数 290 浏览 0 评论 0原文

我正在尝试检测文本区域何时变满以创建分页效果。但是,使用 .length 属性不起作用,因为长单词会“跳”到新行。

| I like to dooo|     displays as  | I like to     | 
| oooodle       |                  | dooooooodle   |

因此最终发生的情况是,在 .length 属性达到文本区域限制之前,文本区域总是耗尽空间。还有其他方法可以检测文本区域的填充度吗? Jquery 解决方案也很好。谢谢。

I am trying detect when a textarea becomes full for creating pagination effect. Using the .length property will not work, however, because long words 'jump' to new lines.

| I like to dooo|     displays as  | I like to     | 
| oooodle       |                  | dooooooodle   |

So what ends up happening is that the textarea always runs out of space before the .length property reaches the textarea limit. Is there any other way to detect textarea fullness? Jquery solutions are fine as well. Thanks.

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

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

发布评论

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

评论(2

请叫√我孤独 2024-11-14 14:55:36

您可以尝试检查文本区域中是否出现滚动条。 这里是执行此操作的简单方法。最初使滚动条比您想要显示的最终高度短一行,然后在按键时检查滚动条是否已出现,然后等待输入下一个空格字符。输入空格字符后,立即执行以下操作:
1.删​​除空格字符,
2.增加文本区域高度一行徘徊(因此滚动条消失),
3. 创建一个新的文本区域并将焦点移至新的文本区域。

更新
这是一个演示。我稍微改变了我的方法,这是代码:

Markup

<textarea class="paginate"></textarea>

JS

$('textarea.paginate').live('keydown', function() {

    // scrollbars apreared
    if (this.clientHeight < this.scrollHeight) {

        var words = $(this).val().split(' ');
        var last_word = words.pop();
        var reduced = words.join(' ');
        $(this).val(reduced);
        $(this).css('height', '65px');

        $(this).after('<textarea class="paginate"></textarea>');
        $(this).next().focus().val(last_word);

    }

});

CSS

.paginate { height: 60px; width: 200px; display: block;}

You can try to check if scrollbars have appeared in your textarea. Here is a simple way to do this. Initially make the scrollbar one line shorter than the ultimate height you want to show, then on keypress check if scrollbars have appeared, then wait for the next space char to be entered. As soon as space char is entered do the following:
1. delete the space char,
2. increase the textarea height one line linger (so scrollbar disappears),
3. create a new textarea and move focus to the new textarea.

Update
Here is a demo. I changed my method a bit and this is the code:

Markup

<textarea class="paginate"></textarea>

JS

$('textarea.paginate').live('keydown', function() {

    // scrollbars apreared
    if (this.clientHeight < this.scrollHeight) {

        var words = $(this).val().split(' ');
        var last_word = words.pop();
        var reduced = words.join(' ');
        $(this).val(reduced);
        $(this).css('height', '65px');

        $(this).after('<textarea class="paginate"></textarea>');
        $(this).next().focus().val(last_word);

    }

});

CSS

.paginate { height: 60px; width: 200px; display: block;}
梦冥 2024-11-14 14:55:36

在运行时,您可以监听textarea的按键事件,将textarea.val()值传递到隐藏的

In runtime, you may listen to the key-press event of the textarea, pass the textarea.val() value into a hidden <pre id="mypre" style="display:none; "></pre>, then get mypre's width or even height $("#mypre").width(). It's your decision how you'll work with the "simulated" width/height.

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