获取文字行数,实现文字超行收起和展开功能
<div id='content'>这是一段文字</div>
function handleShowTxt () {
const pxToNum = (px) => Number(px.replace('px', '')) // 处理px
const div = document.getElementById('content')
const clientHeight = div.clientHeight // 获取div的高度
const lineHeight = window.getComputedStyle(div, null).lineHeight // 获取每行文字的高度
const rowNum = clientHeight / pxToNum(lineHeight)
return rowNum // 文字行数
}
handleShowTxt()
最后通过判断行数实现是否收起功能即可。