文本区域值高度
我有一个 textarea,其 css 高度设置为 85px。用户可能会在该文本区域内键入内容行,我想知道文本/值的高度,而不是文本区域本身。
有没有办法检查内部文本的高度(包括换行符)?
I have a textarea that has a set css height of 85px. A user might type lines of content within that textarea, and I would like to know how high the text/val is, NOT the text area itself.
Is there a way to check the height of the inside text including line breaks?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会将文本区域的内容复制到具有相同宽度的另一个元素中,隐藏,然后获取其高度。
I would duplicate the content of the textarea into another element with the same width, hidden, and then get its height.
这就是我要做的,尽管显然标记只是通用的,需要根据您的需要进行修改:
html
CSS
jQuery:
JS Fiddle 演示。
可以对此进行修改,以根据需要动态添加和删除
.tmp
div
。但我试图让它尽可能简单,所以我将其硬编码。参考文献:
keyup()
。val()
。next()
。prev()
。text()
。outerHeight()
(和height()
)。This is how I'd do it, though obviously the mark-up is only generic and to be amended to your needs:
html
CSS
jQuery:
JS Fiddle demo.
This could be amended to dynamically add, and remove, the
.tmp
div
s as required. But I was trying to keep this as simple as possible, so I left it hard-coded.References:
keyup()
.val()
.next()
.prev()
.text()
.outerHeight()
(andheight()
).我不确定你为什么要这样做。如果您只是想增加框的大小以匹配文本,请使用多种扩展之一,例如:
http ://unwrongest.com/projects/elastic/
如果这不是您想要的,您可以查看源代码,看看他们如何实现这一点,并模仿它。
I'm not sure exactly why you're wanting to do this. If you are just trying to have the box increase in size to match the text, use one of many extensions such as:
http://unwrongest.com/projects/elastic/
If this isn't what you're looking for, you can have a look at the source and see how they achieve this, and mimic it.