gpt4 book ai didi

javascript - 在 div 中找到文本换行的行数

转载 作者:太空宇宙 更新时间:2023-11-03 21:09:19 26 4
gpt4 key购买 nike

是否有任何方法可以使用 word-break:break-all css 属性找到文本中断的行数。假设我给了一个 div 作为

<div>Sample text to verify how many lines the text got broken</div>

CSS 是

div{
width: 100px;
word-break: break-all;
}

现在浏览器将显示如下,我想知道它从文本中断开了多少行。

请帮助我如何实现这一目标。

enter image description here

最佳答案

您可以根据行高计算容器的大小:

var getLines = function ($container) {
var linehight = parseFloat(window.getComputedStyle($container[0], null).getPropertyValue("line-height"), 10);
var boxHeight = $container.height();
var numberOfLines = Math.round(boxHeight / linehight);
return numberOfLines;
};

//To use it just call the function and pass in your class/selector
var numberOfLines = getLines($('.my-container'));

关于javascript - 在 div 中找到文本换行的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48691284/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com