简体   繁体   English

如何在contentEditable或其他地方找到通过换行创建的换行符?

[英]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.

相关问题 在textarea中找到包含ARABIC文字的“换行符” - finding “line-breaks” in textarea that is word-wrapping ARABIC text 我可以在可自动换行的div标签中用换行符得到“换行符”吗? - Can I get the 'line breaks' in a contenteditable div-tag that's word-wrapping? 我可以通过JavaScript使用浏览器的自动换行吗? - Can I use the browser's word-wrapping from JavaScript? 我怎样才能让浏览器允许行内元素之间的换行符(它们之间没有空格)? - How can I get the browser to allow line-breaks between inline elements (with no spaces between them)? 查找某些/特定的换行符,同时忽略其他换行符 - Find certain/specific line-breaks while ignoring others 设置contenteditable和覆盖换行符 <br /> 代替 <div> 在元素的末尾不起作用 - Setting contenteditable and overwriting line-breaks to <br /> instead of <div> does not work at the end of the element 用jQuery检测换行符? - detecting line-breaks with jQuery? 如何通过 contenteditable div 输入保留附加到列表的元素的换行符(格式)? - How can I keep the line breaks (formatting) of an element appended to a list via a contenteditable div input? 如何使用更多行的文本向textarea添加换行符 - How to add line-breaks to a textarea with text over more lines 检测无序列表中的换行符 - Detecting line-breaks in an unordered list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM