开发者

Fitting text into a fixed-size div

I have a fixed size div wh开发者_如何学JAVAich I need to fill with text. I need to only fill it with as much text will fit into it, and I need to know the offset in the string where it cut off.

Right now I am doing it by saving the div's current height then setting the div's height to auto, taking increasingly long substrings of the string and putting them into the div, and stopping when the div's height gets bigger than the original height. This works, but it's pretty inefficient.

Is there any better way to do this? And by better I mean faster.


Well, I came up with this: http://jsfiddle.net/r5Njr/. That should fit the text into the div. If you want to be fancy, you can change the last 2 or 3 characters to a ...


Maybe I don't get the question correctly but why not use substr and fetch an exact characters number?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜