How can I measure how many characters will fit the width of the document?

前端 未结 3 1633
故里飘歌
故里飘歌 2020-12-19 07:42

I need to write, in JavaScript (I am using jQuery), a function that is aware of the number of characters that fit in a line of the browser window. I\'m using a monospace fo

3条回答
  •  不思量自难忘°
    2020-12-19 08:10

    There is no easy way to do this in HTML.

    However, if you really must know, you can probably figure it out with some really ugly javascript:

    First, create a

    with the width you need, put some characters in and request its .height() in pixels. Then, keep adding characters to it and keep checking the .height(), when the height of the
    changes you know it has grown to fit a new line of text.

提交回复
热议问题