[英]How can I find the line-breaks created by word-wrapping in contentEditable or elsewhere?
I'm trying to find the actual number of lines currently displayed to the user by a browser's layout engine. 我正在尝试查找浏览器的布局引擎当前显示给用户的实际行数。 Finding 'hard' breaks, <'br'> tags etc, is easy enough, but I can't find a way to see, in code, what I can count onscreen.
查找“硬”中断,<'br'>标签等非常容易,但是我找不到在代码中看到我可以在屏幕上计数的方法。
I have searched here, and there are a few questions/answers implying it can't be done, or is at the least very involved, but they are all several years out of date, and perhaps things have changed. 我在这里搜索过,并且有一些问题/答案暗示它无法完成,或者至少涉及很多,但是它们都已经过时了几年,也许情况已经改变了。
textContent doesn't do it, and using the div's height doesn't work in this application. textContent不会执行此操作,因此在此应用程序中无法使用div的高度。 Maybe there's a jQuery way I'm unaware of?
也许有一种我不知道的jQuery方式?
Divide the height of the whole thing by the height of one character. 将整个事物的高度除以一个字符的高度。 This method works in IE, Safari, FF and Chrome, though the situation is fairly simple, only one size of font etc, so could do with a more thorough workout before being declared The Right Way;
这种方法可以在IE,Safari,FF和Chrome中使用,尽管情况非常简单,只有一个字体大小等,所以可以在声明正确方法之前进行更彻底的锻炼。 it needs empty node checks etc.
它需要空节点检查等。
function getLineCount(node) {
if (node) {
var range = document.createRange();
range.setStart(node, 0);
range.setEnd(node, 1);
var h1 = range.getBoundingClientRect().height;
range.setEnd(node, node.length);
return Math.round(range.getBoundingClientRect().height / h1);
}
};
Is the Tumbleweed Badge also known as crickets? 风滚草徽章也被称为吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.