问题
I have a textfield in a grid in which I wish to turn off text wrapping, but display a tooltip with the full text if wrapping is required. So, how could I detect if a field needs to wrap?
I tried to query the field's length, but it was either in em
or in px
based on if the user resized it. How could I get the wrapping status?
回答1:
An option to detect if the text wraps - create (with jQuery, for instance) an invisible span with the same font settings and white-space: nowrap
, set it's text
to the field's content and check if the span's width is bigger than the field's width (in pixels, obtained via width()
)
Working fiddle: http://jsfiddle.net/TR98y/1/
回答2:
Perhaps you can check the height (via scrollHeight) of the text container. It should increase when things start wrapping.
The only other alternative I can think of is setting overflow-x:hidden and then dettecting the overflow somehow.
来源:https://stackoverflow.com/questions/7609564/how-to-detect-if-a-text-field-needs-to-wrap-in-javascript-dojo