是“ScrollWidth”吗?跨度不适用于Chrome的属性? [英] Is "ScrollWidth" property of a span not working on Chrome?

查看:165
本文介绍了是“ScrollWidth”吗?跨度不适用于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 ]

要点:


  1. sw仅在元素的显示不是内联时才有效无

    ff默认情况下将显示设置为而c将其设置为内联
    所以你不会得到c。如果你需要隐藏元素,只需使用:

    visibility:hidden 而不设置显示对于ff和c。

  2. 如果您在c中设置了可见性,请使用 style.width 元素而不是sc。这有效。

  1. sw works only when the element's display is not inline and none.
    ff sets the display to block by default while c sets it to inline. So you would not get sw in c. If you need the element to be hidden anyways just use:
    visibility:hidden without setting the display for both ff and c.
  2. If u have set the visibility in c, use style.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

了解文本行在HTML中用于自动换行和其他应用程序的宽度

适用于IE7,8,9,FF,Chrome。

Worked for IE7,8,9 , FF, Chrome.

这篇关于是“ScrollWidth”吗?跨度不适用于Chrome的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆