检测文本是否溢出 [英] Detect if text has overflowed
本文介绍了检测文本是否溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何检测文本是否溢出?例如,以下文本的长度超出了div容器允许的长度.如何在javascript中检测到这一点?
How can you detect if text has overflown? For example, the following text is longer than it's div container allows. How can I detect this in javascript?
<div style="max-width: 100px; white-space:nowrap; overflow: hidden;">
Lorem ipsum dolor sit amet, consectetur adipisicing elit
</div>
推荐答案
如果您使用的是jQuery,则可以尝试将div的宽度与其scrollWidth进行比较.
If you are using jQuery, you can try comparing the div's width to its scrollWidth.
if ($('#div-id')[0].scrollWidth > $('#div-id').innerWidth()) {
//Text has over-flown
}
这篇关于检测文本是否溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文