designMode iFrame 获取光标位置

发布于 2024-12-23 02:16:30 字数 403 浏览 1 评论 0原文

我想获取可编辑 iFrame 的光标位置(使用 designMode)。这是我到目前为止的代码:

document.getElementById('iframe_id').contentWindow.document.getSelection().getRangeAt(0)

从那里获取属性 startOffset 获取从该行开头开始的字符数,但不是从 iFrame 文档开头开始的字符数。我想获取相对于文档开头的光标位置。

请注意:我设置光标位置感兴趣;我只是想得到它。

最好,我希望修复程序能够与 Chrome/Safari/Firefox 兼容;无需与 IE 兼容。

任何帮助将不胜感激。

I want to get the cursor position of an editable iFrame (using designMode). Here is the code I have so far:

document.getElementById('iframe_id').contentWindow.document.getSelection().getRangeAt(0)

From there, getting the property startOffset gets the number of characters from the beginning of that line, but not from the beginning of the iFrame document. I would like to get the cursor position relative to the beginning of the document.

Please note: I am not interested in setting the cursor position; I just want to get it.

Preferably, I would like the fix to be compatible with Chrome/Safari/Firefox; compatibility with IE is not necessary.

Any help would be greatly appreciated.

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

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

发布评论

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

评论(2

诗化ㄋ丶相逢 2024-12-30 02:16:30

以下内容基于此答案,但经过修改以适用于任何文档中的选择(例如 iframe 中的选择)。该答案中提出的关于这种方法天真的警告仍然适用。

function getCaretCharacterOffsetWithin(element) {
    var doc = element.ownerDocument || element.document;
    var win = doc.defaultView || doc.parentWindow;
    var sel, range, preCaretRange, caretOffset = 0;
    if (typeof win.getSelection != "undefined") {
        sel = win.getSelection();
        if (sel.rangeCount) {
            range = sel.getRangeAt(0);
            preCaretRange = range.cloneRange();
            preCaretRange.selectNodeContents(element);
            preCaretRange.setEnd(range.endContainer, range.endOffset);
            caretOffset = preCaretRange.toString().length;
        }
    } else if ( (sel = doc.selection) && sel.type != "Control") {
        range = doc.selection.createRange();
        preCaretRange = doc.body.createTextRange();
        preCaretRange.moveToElementText(element);
        preCaretRange.setEndPoint("EndToEnd", textRange);
        caretOffset = preCaretTextRange.text.length;
    }
    return caretOffset;
}

用法示例:

var iframe = document.getElementById("foo");
var iframeBody = (iframe.contentDocument || iframe.contentWindow.document).body;
alert( getCaretCharacterOffsetWithin(iframeBody) );

The following is based on this answer but is modified to work with selections in any document (such as one in an iframe). The same caveats about the naïveté of this approach laid out in that answer still apply.

function getCaretCharacterOffsetWithin(element) {
    var doc = element.ownerDocument || element.document;
    var win = doc.defaultView || doc.parentWindow;
    var sel, range, preCaretRange, caretOffset = 0;
    if (typeof win.getSelection != "undefined") {
        sel = win.getSelection();
        if (sel.rangeCount) {
            range = sel.getRangeAt(0);
            preCaretRange = range.cloneRange();
            preCaretRange.selectNodeContents(element);
            preCaretRange.setEnd(range.endContainer, range.endOffset);
            caretOffset = preCaretRange.toString().length;
        }
    } else if ( (sel = doc.selection) && sel.type != "Control") {
        range = doc.selection.createRange();
        preCaretRange = doc.body.createTextRange();
        preCaretRange.moveToElementText(element);
        preCaretRange.setEndPoint("EndToEnd", textRange);
        caretOffset = preCaretTextRange.text.length;
    }
    return caretOffset;
}

Example usage:

var iframe = document.getElementById("foo");
var iframeBody = (iframe.contentDocument || iframe.contentWindow.document).body;
alert( getCaretCharacterOffsetWithin(iframeBody) );
恏ㄋ傷疤忘ㄋ疼 2024-12-30 02:16:30

这对我有用

function getCaretPosition() {
var element = document.idEditbox.document.body; // just my content IFRAME
var doc = element.ownerDocument || element.document;
var win = doc.defaultView || doc.parentWindow;
var sel, range, preCaretRange, caretOffset = 0;
if (typeof win.getSelection != "undefined") {
    sel = win.getSelection();
    if (sel.rangeCount) {
        range = sel.getRangeAt(0);
        preCaretRange = range.cloneRange();
        preCaretRange.selectNodeContents(element);
        preCaretRange.setEnd(range.endContainer, range.endOffset);
        caretOffset = preCaretRange.toString().length;
    }
} else if ( (sel = doc.selection) && sel.type != "Control") {
    range = doc.selection.createRange();
var tempRange = range.duplicate();
    preCaretRange = doc.body.createTextRange();
    preCaretRange.moveToElementText(element);
    preCaretRange.setEndPoint("EndToEnd", tempRange);
    caretOffset = preCaretRange.text.length;
}
return caretOffset;

}

This worked for me

function getCaretPosition() {
var element = document.idEditbox.document.body; // just my content IFRAME
var doc = element.ownerDocument || element.document;
var win = doc.defaultView || doc.parentWindow;
var sel, range, preCaretRange, caretOffset = 0;
if (typeof win.getSelection != "undefined") {
    sel = win.getSelection();
    if (sel.rangeCount) {
        range = sel.getRangeAt(0);
        preCaretRange = range.cloneRange();
        preCaretRange.selectNodeContents(element);
        preCaretRange.setEnd(range.endContainer, range.endOffset);
        caretOffset = preCaretRange.toString().length;
    }
} else if ( (sel = doc.selection) && sel.type != "Control") {
    range = doc.selection.createRange();
var tempRange = range.duplicate();
    preCaretRange = doc.body.createTextRange();
    preCaretRange.moveToElementText(element);
    preCaretRange.setEndPoint("EndToEnd", tempRange);
    caretOffset = preCaretRange.text.length;
}
return caretOffset;

}

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