JavaScript - Visible Text of a DIV

前端 未结 5 1934
北恋
北恋 2020-12-01 18:20
----------------------------------------------------
| This is my text inside a div and I want the overf|low of the text to be cut
----------------------------------         


        
5条回答
  •  渐次进展
    2020-12-01 18:58

    You're trying to force a CSS problem into JavaScript. Put the hammer away and get out a screwdriver. http://en.wiktionary.org/wiki/if_all_you_have_is_a_hammer,_everything_looks_like_a_nail

    Solving the answer of character count is probably irrelevant if you take a step back. The last character could be only partially visible, and character count is drastically different given font size changes, the difference of width between W an i, etc. Probably the div's width is more important than the character count in the true problem.

    If you're still stuck on figuring out the characters visible, put a span inside the div around the text, use the css provided in other answers to this question, and then in JavaScript trim one character at a time off the string until the span's width is less than the div's width. And then watch as your browser freezes for a few seconds every time you do that to a big paragraph.

提交回复
热议问题