我想用JavaScript来计算字符串的宽度.这是否可以在不使用等宽字体的情况下实现?
如果它不是内置的,我唯一的想法是为每个字符创建一个宽度表,但这是非常不合理的,特别是支持Unicode和不同的类型大小(以及所有浏览器).
这是一个例子:
<div class="parent">
Contrary to popular belief Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old.
</div>
Run Code Online (Sandbox Code Playgroud)
这个div的宽度是200px,我需要抓住我在浏览器中看到的最后一行,用一个跨度包装它.比如<span>it over 2000 years old.</span>在我的情况下.
这有可能与jquery/javascript?或者至少得到这个"最后"行的长度.
编辑:我想我找到了一个好方法:https://jsfiddle.net/qqkxyq42/2/