js如何获取div中文字的行数?
如图,应该分别获取到一行文字、两行文字,有没有办法获取到呢?
`function countLines(ele) {
var styles = window.getComputedStyle(ele, null);
var lh = parseInt(styles.lineHeight, 10);
var h = parseInt(styles.height, 10);
var lc = Math.round(h / lh);
console.log('line count:', lc, 'line-height:', lh, 'height:', h);
return lc;
}`
countLines(document.querySelector('.desc[_v-36f926b2]'))
10 回答11.2k 阅读
5 回答4.8k 阅读✓ 已解决
4 回答3.1k 阅读✓ 已解决
2 回答2.7k 阅读✓ 已解决
3 回答5.1k 阅读✓ 已解决
3 回答1.9k 阅读✓ 已解决
2 回答4.7k 阅读✓ 已解决
没有直接办法,你只能根据元素高度和每行高度做除法,网上搜下就有答案了