如何在不更改 DOM 的情况下计算文本选择的高度

发布于 2024-11-26 17:59:27 字数 251 浏览 1 评论 0原文

我正在使用范围来操作选定的文本。我想计算从某人开始选择文本到完成选择的高度。

我尝试了跨度到所选范围的开头和结尾,我可以准确地计算出高度形式,但它会更改 DOM 并阻止我进行其他一些范围操作,例如突出显示先前选择的文本。

我还尝试收集 mosedown 和 mosueup 位置的位置,但我需要从所选文本的顶部到释放选择的文本底部的准确高度,但情况并非总是如此。

所以我想知道是否有一种方法可以在不改变 DOM 的情况下计算文本选择的高度?

I am using the Range to manipulate selected text. I would like to calculate the height from where someone started selecting text to where they finished.

I have tried a span to the beginning and end of the selected range and I can accurately calculate the height form that, but it changes the DOM and prevents me form doing some other range manipulations like highlighting previously selected text.

I have also tried collecting the position of the mosedown and mosueup positions but I need an accurate height from the top of the text selected to the bottom of the text where the selection was released and thats not always the case.

So I was wondering if there was a way to calculate the height of a text selection without changing the DOM?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅沫记忆 2024-12-03 17:59:27

这取决于您需要处理哪些浏览器。下面的函数适用于 IE >= 4 以及支持 Range 中的 getClientRects() 的浏览器(Firefox >= 4、2009 年以来的 WebKit、Opera)。如果您需要支持早期浏览器,则需要修改 DOM,实际上只要将 DOM 恢复到之前的状态就可以了。

jsFiddle: http://jsfiddle.net/W84yW/

代码:

function getSelectionHeight() {
    var selHeight = 0;
    if (document.selection && document.selection.type == "Text") {
        var textRange = document.selection.createRange();
        selHeight = textRange.boundingHeight;
    } else if (window.getSelection) {
        var sel = window.getSelection();
        if (sel.rangeCount > 0) {
            var range = sel.getRangeAt(0);
            if (!range.collapsed && range.getClientRects) {
                var startRange = range.cloneRange();
                startRange.collapse(true);
                var selTop = startRange.getClientRects()[0].top;
                startRange.detach();
                var endRange = range.cloneRange();
                endRange.collapse(false);
                selHeight = endRange.getClientRects()[0].bottom - selTop;
                endRange.detach();
            }
        }
    }
    return selHeight;
}

It depends which browsers you need to deal with. Here's a function that will work in IE >= 4 and browsers that support getClientRects() in Range (Firefox >= 4, WebKit since 2009, Opera). If you need support for earlier browsers, you'll need to modify the DOM, which is actually fine so long as you restore the DOM to its previous state.

jsFiddle: http://jsfiddle.net/W84yW/

Code:

function getSelectionHeight() {
    var selHeight = 0;
    if (document.selection && document.selection.type == "Text") {
        var textRange = document.selection.createRange();
        selHeight = textRange.boundingHeight;
    } else if (window.getSelection) {
        var sel = window.getSelection();
        if (sel.rangeCount > 0) {
            var range = sel.getRangeAt(0);
            if (!range.collapsed && range.getClientRects) {
                var startRange = range.cloneRange();
                startRange.collapse(true);
                var selTop = startRange.getClientRects()[0].top;
                startRange.detach();
                var endRange = range.cloneRange();
                endRange.collapse(false);
                selHeight = endRange.getClientRects()[0].bottom - selTop;
                endRange.detach();
            }
        }
    }
    return selHeight;
}
就是爱搞怪 2024-12-03 17:59:27

我将获取选定的文本并将其放入具有相同宽度和相同样式的隐藏 div 中,并获取其高度。

I would get the selected text and place it into an hidden div with the same width and same styles, and get its height.

顾铮苏瑾 2024-12-03 17:59:27

我不认为隐藏的 DIV 方法会起作用。许多浏览器要么没有高度,要么对于像这样隐藏的元素有高度=0。

也许如果您克隆 DOM 的该部分并将其添加到文档中并具有一些疯狂的位置(例如 left:-1000px),您就可以获得该值。

无论如何,我认为如果 Range 对象不为您提供此数据,您在使用变通方法获得一致结果时会遇到问题。

I don't think the hidden DIV approach will work. Many browsers either won't have a height, or will have a height=0 for elements that are hidden like that.

Perhaps if you cloned that portion of the DOM and added it to the document with some crazy position (like left: -1000px), you could get the value.

Regardless, I think you're having to have problems getting consistent results using workarounds if the Range object doesn't provide this data for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文