找回密码
 FreeOZ用户注册
查看: 1412|回复: 12
打印 上一主题 下一主题

[IT技术] javascript question: text width

[复制链接]
跳转到指定楼层
1#
发表于 3-10-2014 07:16:06 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?FreeOZ用户注册

x
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
回复  

使用道具 举报

2#
发表于 3-10-2014 07:39:20 | 只看该作者
should use css.not js
回复  

使用道具 举报

3#
 楼主| 发表于 3-10-2014 07:44:54 | 只看该作者
本帖最后由 DDD888 于 3-10-2014 07:46 编辑


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.
回复  

使用道具 举报

4#
发表于 3-10-2014 08:33:10 | 只看该作者
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
回复  

使用道具 举报

5#
发表于 3-10-2014 08:47:00 | 只看该作者
http://www.w3schools.com/cssref/pr_text_white-space.asp

nowrap咯,拿text的长度算不准的,你要知道字号什么的,思路是不要换行。
回复  

使用道具 举报

6#
 楼主| 发表于 3-10-2014 09:00:24 | 只看该作者
周星星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?
回复  

使用道具 举报

7#
发表于 3-10-2014 09:01:49 | 只看该作者
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
回复  

使用道具 举报

8#
 楼主| 发表于 3-10-2014 09:02:23 | 只看该作者
mason00 发表于 3-10-2014 08:47
http://www.w3schools.com/cssref/pr_text_white-space.asp

nowrap咯,拿text的长度算不准的,你要知道 ...

不能换行,我知道字号,我设置字号啦

我现在是随便假设字符宽度是18,但我想得到个通用的算法
回复  

使用道具 举报

9#
发表于 3-10-2014 09:13:26 | 只看该作者
this might help you.
http://blog.mastykarz.nl/measuri ... s-using-javascript/
but again,  it's a design problem
回复  

使用道具 举报

10#
发表于 3-10-2014 09:15:56 | 只看该作者
or this
http://stackoverflow.com/questio ... n-javascript-jquery

it's just fucking pain of us..
回复  

使用道具 举报

11#
发表于 3-10-2014 09:40:58 | 只看该作者
DDD888 发表于 3-10-2014 09:02
不能换行,我知道字号,我设置字号啦

我现在是随便假设字符宽度是18,但我想得到个通用的算法

nowrap就是不换行呀,你的目的就是别换行对伐啦
回复  

使用道具 举报

12#
 楼主| 发表于 3-10-2014 09:56:32 | 只看该作者
mason00 发表于 3-10-2014 09:40
nowrap就是不换行呀,你的目的就是别换行对伐啦

Yes, and fit the element width same as text length.
回复  

使用道具 举报

13#
发表于 3-10-2014 20:37:20 | 只看该作者
css

padding: 10px;
or
padding-right: 10px;
回复  

使用道具 举报

您需要登录后才可以回帖 登录 | FreeOZ用户注册

本版积分规则

小黑屋|手机版|Archiver|FreeOZ论坛

GMT+10, 24-9-2025 04:00 , Processed in 0.042814 second(s), 28 queries , Gzip On, Redis On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表