HTML textarea: use JavaScript to get wrapped text?

前端 未结 5 1609
伪装坚强ぢ
伪装坚强ぢ 2021-01-03 15:31

If I\'ve got a textarea like this:


Is there any way, using JavaScript, t

5条回答
  •  暖寄归人
    2021-01-03 16:10

    I'm assuming that you know the dimensions of the text area (if not, you can probably just get them from the DOM).

    Use this to split the string. Go to the xth char (in your example the 5th), and see if the next char is a space. If so (or if there are no spaces in the preceding segment), this is where you split. Otherwise, you find the preceding space and split there.

    Wrap this in a function and you could add in some recursion to find the nth line.

提交回复
热议问题