开发者

How to get the last line in a textarea?

How to get the last line in a textarea in case lines are wrapped into 开发者_开发知识库"new lines" automatically, that is, without "/n" but because of the width of the textarea?


I'm not sure I got the question correctly, but... take the value of TextArea with jQuery as string with something like $('#textareaid').val(), then split string by newlines like split("\n");?

So you would get var lines = $('#textareaid').val().split("\n") an array of lines in a textarea. Then you just take latest one like lines[lines.length-1].


Just a quick idea:

  • Style a div with display: inline-block; and the same width and font as the textarea
  • Copy the last n characters of your textarea string into that div.
  • Increase n until the height of your div increases. That indicates a line-wrap has taken place.
  • Now you know the amount of characters off the end of your string that fit into a line without wrapping is n-1.


I think I understand what you want. You want the last line and when the last real line is w rapped in to a new one, you want that last peace.

This really isn't easy (as far as I know). What you need to do is split the lines by new-lines break (\n) and get the last line. Then you need to calculate how long that line is (you can't know for sure what font a browser uses). With that size you need to compare it to the size of the textarea. Problem here is that you can't divide that line by the textarea width, because words that are to long for a line are wrapped to the next line. So you need to calculate every word too.

I say good luck :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜