如何使用JavaScript或jQuery获取文本宽度(以像素为单位)? [英] How do I get text width in pixels, using JavaScript or jQuery?

查看:85
本文介绍了如何使用JavaScript或jQuery获取文本宽度(以像素为单位)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得textarea文字宽度。如何获得以像素为单位的文字宽度?

I want to get textarea text width. How do I get text width in pixels?

我不想要文字长度。

推荐答案

只需将文本包装在(不可见) span 中,然后使用 jQuerys .width()获取宽度。

Simply wrap the text in a (invisible) span and use jQuerys .width() to get the width.

请注意,,就像其他人一样说,使用 div ,因为它是一个块元素 - 并且根据插入的位置/方式,块元素可以自动扩展到完整的可用宽度并给出糟糕的结果。

Note that you should not, like others said, use a div as it's a block-element - and depending on where/how you insert that, a block-element may auto-expand to the full available width and give bad results.

这篇关于如何使用JavaScript或jQuery获取文本宽度(以像素为单位)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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