简体   繁体   English

如何使用display table-cell获取文本的宽度和高度而不是span

[英]How to get width and height of text not the span with display table-cell

How to find width and height of a text not the span's having display:table-cell . 如何找到文本的宽度和高度而不是span's display:table-cell In side of a div with display:table . display:tablediv一侧display:table

 <div style="display: table; width: 50px; height: 343.559px;"><span class="nodeText" style="display: table-cell;vertical-align: middle;text-overflow: ellipsis;font-family: Arial;font-size: 10px;font-style: normal;font-weight: normal;color: black;opacity: 1;">Desktop 1.43k (53.34%)</span></div> 

Text width and height is 文字宽度和高度是

width is approx 45px 宽度约为45px
height is approx 35px 高度约为35px

https://developer.mozilla.org/en-US/docs/Web/API/Element/clientWidth https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetWidth https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle https://developer.mozilla.org/en-US/docs/Web/API/Element/clientWidth https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetWidth https:// developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle

 var $e = document.getElementById("e"); var $a = document.getElementById("a"); // Thses are for the span itself... // Only inner width and padding console.log($e.clientWidth,$e.clientHeight); // also scrollbar if presented and ... console.log($e.offsetWidth,$e.offsetHeight); // get the width after any computing necessary console.log( $e.getBoundingClientRect() ); var style = window.getComputedStyle( $e ); console.log( style.font ); // Now combine them to measure string in pixels var $canvas= document.createElement("canvas"); var ctx = $canvas.getContext("2d"); ctx.font = style.font; var txt = $e.innerText; var measures = ctx.measureText(txt); console.log( ctx.font ); console.log( measures ); $a.style.height = Math.ceil(measures.width / $e.clientWidth) * parseInt(style.lineHeight) + 'px'; 
 div, #e { position:relative; } #a { display:block;width:3px;background:red; } 
 <!-- changed the size to fit in preview --> <div style="display: table; width: 50px; height: 47px;"><span id="e" class="nodeText" style="display: table-cell;vertical-align: middle;text-overflow: ellipsis;font-family: Arial;font-size: 10px;font-style: normal;font-weight: normal;color: black;opacity: 1;">Desktop 1.43k (53.34%)</span> <span style="display: table-cell;vertical-align: middle;"> <span id="a"></span> </span> </div> 

Strange nobody recommended getBoundingClientRect : 奇怪没有人推荐getBoundingClientRect

fiddle 小提琴

var span = document.getElementsByTagName("span")[0],
        rect = span.getBoundingClientRect();
span.parentNode.nextElementSibling.textContent = "width: " + (rect.width|0) 
+ "\nheight: " + (rect.height|0); 

PS: I see that you meant the client area without the padding. PS:我看到你的意思是没有填充的客户区。 And that was not clear in your question. 你的问题并不清楚。 Use 2 spans so that you end up with client areas. 使用2个跨度,以便最终得到客户区域。

HERE - width: 40 height: 32 这里 - 宽度:40高:32

You can have a check for the div if it has a display:table then you can iterate through it and find the span with display:table-cell property. 你可以检查div是否有display:table然后你可以迭代它并找到带有display:table-cell属性的span

let divs = document.querySelectorAll('div');

divs.forEach(div => {
    if(div.style.display === 'table'){
      let spans = document.querySelectorAll('span');
      spans.forEach(spn => {
        if(spn.style.display === 'table-cell'){
          let theSpan = spn.getBoundingClientRect();
            console.log(theSpan.width, theSpan.height);
        }
      });
    }
})

If you don't want the height of the span to be the height of the div (you want the space that the actual text takes up) you can extra wrap the text: 如果您不希望跨度的高度为div的高度(您希望实际文本占用的空间),则可以额外包装文本:

 const span = document.querySelector("span>span"); console.log( span.getBoundingClientRect() ) 
 <div style="display: table; width: 50px; height: 343.559px;"> <span class="nodeText" style="display: table-cell;vertical-align: middle;text-overflow: ellipsis;font-family: Arial;font-size: 10px;font-style: normal;font-weight: normal;color: black;opacity: 1;"> <span>Desktop 1.43k (53.34%)</span> </span> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获得跨度中文本的高度和宽度而不是整个跨度的宽度和高度 - How to get the height and width of text in a span not the whole span width and height 如何在 li 内使用表格单元格显示格式化按钮文本? - How to format buttons text with table-cell display inside li? Chrome 30和类型的宽度显示:table-cell? - Chrome 30 and the width of elements of type display: table-cell? 如何截断表格单元格中的文本? - how to truncate text in table-cell? 如何在IE中获得display:table-cell支持? 任何纯JavaScript或jQuery解决方法? - How to get display:table-cell support in IE? any pure javascript or jQuery workaround? 如何更改jquery .toggle()以使用display:table-cell? - How to change jquery .toggle() to use display:table-cell? 在Firefox中使用display:table-cell创建100%高的div时出现问题 - Issues creating 100% height divs using display: table-cell in Firefox 当单元格内的文本时如何获得表格单元格的正确高度 - How to get the correct height of Table cell when text inside the cell 单击时使用“显示:表格单元格”展开 div? - Expand div with "display: table-cell" on click? 边距/宽度在div元素位置上的排列不正确: 显示:表格单元格; 组 - Margin/width not rending properly on a div element position: fixed; display: table-cell; set
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM