How to detect if a text-field needs to wrap in JavaScript/dojo?

亡梦爱人 提交于 2019-12-23 19:00:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!