首页 > 解决方案 > 根据大小和字体查找文本高度

问题描述

我需要一个功能与 ctx.measureText(string).width 完全相同,但具有字符串的高度。当我发现 ctx.measureText(string).height 不存在时,我感到非常惊讶。似乎很奇怪,会有一个宽度而不是高度......

(请不要使用 jQuery)

标签: javascriptfunctionfontsheightmeasure

解决方案


您可以clientHeight使用元素的属性来获取它。

const p = document.querySelector('p');

function findHeight(element) {
    return element.clientHeight;
} 

console.log(findHeight(p));
//returns paragraph height in px

推荐阅读