admin管理员组文章数量:1320670
A bit of (JS-modified) HTML might look like this:
<div style="width: 50px;">1,234,567</div>
How do I detect if the text node is wider than the container?
A bit of (JS-modified) HTML might look like this:
<div style="width: 50px;">1,234,567</div>
How do I detect if the text node is wider than the container?
Share Improve this question asked Aug 14, 2012 at 19:35 Niet the Dark AbsolNiet the Dark Absol 325k85 gold badges474 silver badges599 bronze badges 6-
I'd guess it'll involve cloning the text node, and dropping it into a container that doesn't have a fixed width, or if you can change the markup, wrap the text content in a
<span>
, and use the width of that instead. – user1106925 Commented Aug 14, 2012 at 19:39 -
possible duplicate of How to detect overflow in div element?. Just use
scrollWidth
instead ofscrollHeight
– Matt Ball Commented Aug 14, 2012 at 19:39 - @MattBall That seems useful, but I don't have hidden overflow on the element. Will I have to add that, or will it work without an overflow setting? – Niet the Dark Absol Commented Aug 14, 2012 at 19:52
-
It works without
overflow: hidden
. jsfiddle/mattball/pYj5P – Matt Ball Commented Aug 14, 2012 at 20:08 - Oh, well that's excellent. If you could make that an answer I'll gladly Accept it. – Niet the Dark Absol Commented Aug 14, 2012 at 20:17
1 Answer
Reset to default 7As inspired by How to detect overflow in div element?:
<!-- Overflowing -->
<div style="width: 50px;">1,234,567</div>
<!-- Not overflowing -->
<div>1,234,567</div>
JavaScript (no jQuery) to detect:
var divs = document.getElementsByTagName('div');
var i, div, overflowing;
for (i=0; i<divs.length; i++) {
div = divs[i];
overflowing = div.offsetWidth < div.scrollWidth;
console.log(overflowing);
}
http://jsfiddle/mattball/pYj5P/
本文标签: javascriptGetting the width of a text nodeStack Overflow
版权声明:本文标题:javascript - Getting the width of a text node - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742063680a2418702.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论