是“ScrollWidth”吗?跨度不适用于Chrome的属性? [英] Is "ScrollWidth" property of a span not working on Chrome?
问题描述
我的内部有一些文字。
I have a span with some text inside it.
document.getElementById('span1').scrollWidth;
在firefox上返回'100'作为scrollWidth但Chrome只返回'0'。 Chrome不支持此scrollWidth属性吗?任何其他不支持相同的浏览器?
on firefox return '100' as the scrollWidth but Chrome returns only '0'. Does Chrome not support this 'scrollWidth' property? Any other browser which doesnt support the same?
如果我需要以像素为单位得到一个句子的长度,还有什么可以解决这个问题呢?(我一直在填充' span1'与句子使用'innerHTML'属性)
Also what would be work around for this if I need to get the length of a sentence in pixels?(I keep populating 'span1' with sentences using 'innerHTML' property)
推荐答案
Firefox和Chrome对css的响应方式有很多不同JS。
[ff = Firefox,c = Chrome,sw = scrollWidth]
There is a lot of difference in how Firefox and Chrome respond to css and js. [ff=Firefox, c=Chrome, sw= scrollWidth ]
要点:
- sw仅在元素的
显示
不是内联
和时才有效无
。
ff默认情况下将显示
设置为块
而c将其设置为内联
。
所以你不会得到c。如果你需要隐藏元素,只需使用:
visibility:hidden
而不设置显示
对于ff和c。 - 如果您在c中设置了
可见性
,请使用style.width
元素而不是sc。这有效。
- sw works only when the element's
display
is notinline
andnone
.
ff sets thedisplay
toblock
by default while c sets it toinline
. So you would not get sw in c. If you need the element to be hidden anyways just use:
visibility:hidden
without setting thedisplay
for both ff and c. - If u have set the
visibility
in c, usestyle.width
of the element instead of sc. This works.
如果您仍想在Chrome上使用sc也隐藏 span
,你可以使用
@AnthonyWJones回答
If you still want to use sc on chrome also for a hidden span
, you could use
@AnthonyWJones answer on
适用于IE7,8,9,FF,Chrome。
Worked for IE7,8,9 , FF, Chrome.
这篇关于是“ScrollWidth”吗?跨度不适用于Chrome的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!