js获取文本的宽度高度

直接上代码

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        div{
            font-size: 14px;
            font-family: "微软雅黑";
        }
    </style>
</head>
<body>
    <div>
        <span>123456789</span>
    </div>
    <script>
        function textSize(fontSize, fontFamily, text) {
            var span = document.createElement('span')
            var dom = document.createElement('div')
            dom.style.width = '100%'
            dom.style.height = '10px'
            dom.style.overflow = 'hidden'
            var result = {}
            result.width = span.offsetWidth
            result.height = span.offsetHeight
            span.style.visibility = 'hidden'
            span.style.fontSize = fontSize
            span.style.fontFamily = fontFamily
            span.style.display = 'inline-block'
            dom.appendChild(span)
            document.body.appendChild(dom)
            if (typeof span.textContent !== 'undefined') {
                span.textContent = text
            } else {
                span.innerText = text
            }
            result.width = parseFloat(window.getComputedStyle(span).width) - result.width
            result.height = parseFloat(window.getComputedStyle(span).height) - result.height
            dom.remove()
            return result
        } 
        var res =  textSize(14, "微软雅黑", "123456789")
        console.log(res.height);
    </script>

</body>

</html>

经测试发现,div的高度为19px,控制台打印出来的结果也是19

JavaScript中,获取HTML元素的实际内容宽度高度有多种方法。以下是几种常见的方式: 1. **innerText 和 innerHTML**: - `textContent` 或 `innerText` 获取文本内容的宽度,适用于文字,不包括标签: ```javascript const element = document.getElementById('yourElement'); const contentWidth = element.textContent.length * element.measureText(' ').width; ``` - `innerHTML` 获取整个元素及其所有子节点的HTML内容,包括宽度高度,但处理起来复杂一些,通常需要额外解析: ```javascript const element = document.getElementById('yourElement'); const boundingRect = element.getBoundingClientRect(); const contentWidth = boundingRect.width; ``` 2. **getBoundingClientRect()**: 这个方法返回一个对象,包含了元素的边框、内边距、外边距以及滚动位置等信息。可以获取元素相对于视口的位置: ```javascript const element = document.getElementById('yourElement'); const contentWidth = element.offsetWidth; const contentHeight = element.offsetHeight; ``` 3. **CSS测量** (仅限特定场景): 使用`window.getComputedStyle()`结合`clientWidth`和`clientHeight`属性,可以获取包含内边距和边框的宽度高度: ```javascript const element = document.getElementById('yourElement'); const computedStyle = window.getComputedStyle(element); const contentWidth = parseInt(computedStyle.getPropertyValue('width')) - parseFloat(computedStyle.getPropertyValue('padding-left')) - parseFloat(computedStyle.getPropertyValue('padding-right')); const contentHeight = parseInt(computedStyle.getPropertyValue('height')) - parseFloat(computedStyle.getPropertyValue('padding-top')) - parseFloat(computedStyle.getPropertyValue('padding-bottom')); ``` 请注意,以上方法可能会受到浏览器渲染差异、用户交互影响(如滚动)以及CSS样式的影响,所以在实际应用中,可能需要进一步调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值