简体   繁体   English

使用来自父元素的javascript计算用户选择的开始和结束位置

[英]Calculate the start and end position of user selection using javascript from a parent element

I'm looking for a way to calculate the start and end position of a users selection from a known parent element. 我正在寻找一种方法来计算来自已知父元素的用户选择的开始和结束位置。 I came across this with some slight modification I was able to get working for FF, but I'm not sure how to do this in IE and I'd love to get some thoughts if my modification is appropriate. 我碰上了这样一些轻微的修改,我能够得到FF工作,但我不知道如何在IE做到这一点,我很想得到一些想法,如果我的修改是适当的。 Big thanks to Tim Down for the original answer. 非常感谢Tim Down的原始答案。

function getBodyTextOffset(node, offset) {
    var sel = window.getSelection();
    var range = document.createRange();
    range.selectNodeContents(document.getElementById('test'));
    range.setEnd(node, offset);
    sel.removeAllRanges();
    sel.addRange(range);
    return sel.toString().length;
}

function getSelectionOffsets() {
    var sel, range;
    var start = 0, end = 0;
    if (window.getSelection) {
        sel = window.getSelection();
        if (sel.rangeCount) {
            range = sel.getRangeAt(sel.rangeCount - 1);
            start = getBodyTextOffset(range.startContainer, range.startOffset);
            end = getBodyTextOffset(range.endContainer, range.endOffset);
            sel.removeAllRanges();
            sel.addRange(range);
            alert(start + ", " + end);
        }
    } else if (document.selection) {
        // IE stuff here
    }
    return {
        start: start,
        end: end
    };
}

I know this has been a while but here is a JSFiddle showing the idea. 我知道这已经有一段时间了,但这里有一个JSFiddle来展示这个想法。 Again it works in FF and Chrome but not IE9. 它再次适用于FF和Chrome,但不适用于IE9。 Ideally I'd like to be able to get the offset from the beginning of the element test. 理想情况下,我希望能够从元素测试的开始获得偏移量。

JavaScript range is a painful topic though here are some good resources... JavaScript范围是一个痛苦的话题,虽然这里有一些很好的资源......

Here are some demos that you can use as reference... 以下是一些可以作为参考的演示......

http://dylanschiemann.com/articles/dom2Range/dom2RangeExamples.html http://dylanschiemann.com/articles/dom2Range/dom2RangeExamples.html

A vague though decent starting point to understand what methods and properties are available... http://help.dottoro.com/ljxsqnoi.php 一个含糊不清但虽然不错的起点,了解可用的方法和属性... http://help.dottoro.com/ljxsqnoi.php

You can discover what contains what using the following JavaScript... 您可以使用以下JavaScript发现包含内容的内容...

for (i in window.getSelection()) {alert('i = '+i);}

When you see an object mentioned you can append it, just make sure you know to also use parenthesis for methods and on rare occasion parameters such as the one below... 当你看到提到的对象你可以附加它时,只要确保你知道也使用括号方法和罕见的场合参数,如下面的那个......

for (i in window.getSelection().getRangeAt(0)) {alert('i = '+i);}

Also there is a VERY nasty range bug in Gecko browsers (eg Firefox) where if you move the mouse just slightly outside of an element though less then 50% of a letter/character that Gecko incorrectly sets the boundary element to that element even though it hasn't been selected. Gecko浏览器(例如Firefox)中还有一个非常讨厌的范围错误,如果你将鼠标稍微移动到一个元素之外,虽然不到50%的字母/字符,Gecko错误地将边界元素设置为该元素,即使它尚未被选中。 It's exceptionally nasty just trying to figure out what it is and Mozilla has actually refused to fix it (as they have with numerous other bugs where they are clearly in the wrong), here is the link for the bug post I made... 只是试图找出它是什么而且Mozilla实际上拒绝修复它是非常讨厌的(因为它们有许多其他错误,它们显然是错误的),这里是我发布的错误帖子的链接...

https://bugzilla.mozilla.org/show_bug.cgi?id=696571 https://bugzilla.mozilla.org/show_bug.cgi?id=696571

Hopefully this will get you moving in the right direction. 希望这会让你朝着正确的方向前进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM