javascript question: text width
Good morning,I have a html element need to set the element width to be little bigger than the text display inside. How can I get the width of the text in p element in javascript/jquery?
TIA should use css.not js 本帖最后由 DDD888 于 3-10-2014 07:46 编辑
周星星1832 发表于 3-10-2014 07:39
should use css.not js
I don't understand what you said. Show me the code of css.
My text is dynamic. It will change based on user action. It is not fixed. So the width is dynamic too. DDD888 发表于 3-10-2014 07:44
I don't understand what you said. Show me the code of css.
My text is dynamic. It will change ...
long story short, make that dame a element a fixed with and block element http://www.w3schools.com/cssref/pr_text_white-space.asp
nowrap咯,拿text的长度算不准的,你要知道字号什么的,思路是不要换行。 周星星1832 发表于 3-10-2014 08:33
long story short, make that dame a element a fixed with and block element
If the text is long, will the element change width based on the text length? DDD888 发表于 3-10-2014 09:00
If the text is long, will the element change width based on the text length?
no....wrap the text then mason00 发表于 3-10-2014 08:47
http://www.w3schools.com/cssref/pr_text_white-space.asp
nowrap咯,拿text的长度算不准的,你要知道 ...
不能换行,我知道字号,我设置字号啦
我现在是随便假设字符宽度是18,但我想得到个通用的算法 this might help you.
http://blog.mastykarz.nl/measuring-the-length-of-a-string-in-pixels-using-javascript/
but again,it's a design problem or this
http://stackoverflow.com/questions/2057682/determine-pixel-length-of-string-in-javascript-jquery
it's just fucking pain of us..
DDD888 发表于 3-10-2014 09:02
不能换行,我知道字号,我设置字号啦
我现在是随便假设字符宽度是18,但我想得到个通用的算法
nowrap就是不换行呀,你的目的就是别换行对伐啦 mason00 发表于 3-10-2014 09:40
nowrap就是不换行呀,你的目的就是别换行对伐啦
Yes, and fit the element width same as text length. css
padding: 10px;
or
padding-right: 10px;
页:
[1]