IE 选择和范围问题

发布于 2024-07-07 04:49:54 字数 363 浏览 5 评论 0原文

我试图获取选择的开始元素和结束元素以及每个选择的偏移量,我在 Firefox 中执行此操作,如下所示:

var delselection = window.getSelection();

var startOffset = delselection.anchorOffset;

var endOffset = delselection.focusOffset;

var startNode = delselection.anchorNode.parentNode;

var endNode = delselection.focusNode.parentNode;

但是我不知道如何在 IE6 中执行此操作,任何人都可以指出我方向正确吗?

I'm trying to get the start element and the end element of a selection and the offset of the selection in each, i do this in firefox as follows:

var delselection = window.getSelection();

var startOffset = delselection.anchorOffset;

var endOffset = delselection.focusOffset;

var startNode = delselection.anchorNode.parentNode;

var endNode = delselection.focusNode.parentNode;

However i have no idea how to do this in IE6, anyone able to point me in the right direction?

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

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

发布评论

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

评论(3

听你说爱我 2024-07-14 04:49:54

文档.选择。

然而,IE 返回的 TextRange 对象与 Firefox/WebKit/W3 的不匹配,并且确定起点和终点的确切位置非常令人沮丧。 根据您对范围的具体操作,您可以使用 range.parentElement()、range.inRange() 或 range.compareEndPoints() 到达某个位置。 对于富文本编辑器,您通常最终会使用极其丑陋的 range.execCommand() 接口。

IE Range 实现非常奇怪,并且与 Mozilla/Webkit/W3 模型不同,因此您通常会在与两者之间的选择有关的所有事情上得到完全不同的代码路径。

document.selection.

However the TextRange object returned by IE does not match Firefox/WebKit/W3's, and determining the exact positions of the start and end points is very frustrating. Depending on what exactly you are doing with the range you may be able to get somewhere with range.parentElement(), range.inRange() or range.compareEndPoints(). For rich text editors you will usually end up using the staggeringly ugly range.execCommand() interface.

The IE Range implementation is so odd and different to the Mozilla/Webkit/W3 model that you typically end up with completely different code paths for everything to do with selections between the two.

泪冰清 2024-07-14 04:49:54

您应该查看 ControlRange 和 < a href="http://msdn.microsoft.com/en-us/library/ms535872(VS.85).aspx" rel="nofollow noreferrer">TextRange IE BOM 对象。

我相信 IE6/7 不支持 AnchorOffset、focusOffset 和 window.getSelection() 。

You should look at the ControlRange and TextRange objects of the IE BOM.

AnchorOffset,focusOffset and window.getSelection() are not supported by IE6/7 I believe.

却一份温柔 2024-07-14 04:49:54

如果您知道选择所在的对象(例如,它是用户正在输入的输入字段,而您希望在他们输入时更改),则以下代码可以解决问题:

var selObj = null;
var selSave = null;
var selSaveEnd = null;

function SaveSelection(obj) {
    if (obj.selectionStart) {
        selObj = obj;
        selSave = obj.selectionStart;
        selSaveEnd = obj.selectionEnd;
    }
    else {
        // Internet Explorer case
        selSave = document.selection.createRange();
    }
}

function RestoreSelection() {
    if (selObj) {
        selObj.focus();
        selObj.selectionStart = selSave;
        selObj.selectionEnd = selSaveEnd;
    }
    else {
        // Internet Explorer case
        selSave.select();
    }
}

If you know the object the selection is in (e.g., it's an input field the user is typing in that you want to change while they're typing), this code does the trick:

var selObj = null;
var selSave = null;
var selSaveEnd = null;

function SaveSelection(obj) {
    if (obj.selectionStart) {
        selObj = obj;
        selSave = obj.selectionStart;
        selSaveEnd = obj.selectionEnd;
    }
    else {
        // Internet Explorer case
        selSave = document.selection.createRange();
    }
}

function RestoreSelection() {
    if (selObj) {
        selObj.focus();
        selObj.selectionStart = selSave;
        selObj.selectionEnd = selSaveEnd;
    }
    else {
        // Internet Explorer case
        selSave.select();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文