js获取文字宽度

必须要在DOM中后才可以获取到宽度

let dom = document.createElement('span');
dom.style.display = 'inline-block'
dom.textContent = '测试文字宽度';
document.body.appendChild(dom)
console.log(dom.clientWidth)
document.body.removeChild(dom)

在这里插入图片描述

发布了96 篇原创文章 · 获赞 15 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_35958891/article/details/103458299
今日推荐