如何在文本区域中获取选择的起点和终点?

Nay*_*iya 17 javascript position cursor internet-explorer-6

我想在文本字段或文本区域中获取所选范围的光标开始和结束位置.我在各种论坛上尝试了很多功能.但是当选择的最后一个字符是新行字符时,JavaScript会在IE6中忽略它.有想法的人吗?

Tim*_*own 34

修订的答复,2010年9月5日

在IE中考虑尾随换行是很棘手的,我还没有看到任何解决方案.但是,它有可能.以下是我之前在此发布的新版本.

请注意,textarea必须具有焦点才能使此功能在IE中正常工作.如有疑问,focus()请先调用textarea的方法.

function getInputSelection(el) {
    var start = 0, end = 0, normalizedValue, range,
        textInputRange, len, endRange;

    if (typeof el.selectionStart == "number" && typeof el.selectionEnd == "number") {
        start = el.selectionStart;
        end = el.selectionEnd;
    } else {
        range = document.selection.createRange();

        if (range && range.parentElement() == el) {
            len = el.value.length;
            normalizedValue = el.value.replace(/\r\n/g, "\n");

            // Create a working TextRange that lives only in the input
            textInputRange = el.createTextRange();
            textInputRange.moveToBookmark(range.getBookmark());

            // Check if the start and end of the selection are at the very end
            // of the input, since moveStart/moveEnd doesn't return what we want
            // in those cases
            endRange = el.createTextRange();
            endRange.collapse(false);

            if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) {
                start = end = len;
            } else {
                start = -textInputRange.moveStart("character", -len);
                start += normalizedValue.slice(0, start).split("\n").length - 1;

                if (textInputRange.compareEndPoints("EndToEnd", endRange) > -1) {
                    end = len;
                } else {
                    end = -textInputRange.moveEnd("character", -len);
                    end += normalizedValue.slice(0, end).split("\n").length - 1;
                }
            }
        }
    }

    return {
        start: start,
        end: end
    };
}
Run Code Online (Sandbox Code Playgroud)


Der*_*會功夫 7

使用RangyAPI和所有的问题都去了哪里 走了走了 ......gone

使用它

阅读文档,或只使用下面的内容.
简单,

var selection = rangy.getSelection(),  // Whole lot of information, supports
                                       // multi-selections too.
    start   = selection.anchorOffset,  // Start position
    end     = selection.focusOffset;   // End position
Run Code Online (Sandbox Code Playgroud)

希望这个api可以帮助你,因为它对处理跨浏览器非常有帮助. ranges


mil*_*dev 6

我需要做一些非常相似的事情并想出这个:

function getSelection(target) {
    var s = {start: 0, end:0};
    if (typeof target.selectionStart == "number"
        && typeof target.selectionEnd == "number") {
        // Firefox (and others)
        s.start = target.selectionStart;
        s.end = target.selectionEnd;
    } else if (document.selection) {
        // IE
        var bookmark = document.selection.createRange().getBookmark();
        var sel = target.createTextRange();
        var bfr = sel.duplicate();
        sel.moveToBookmark(bookmark);
        bfr.setEndPoint("EndToStart", sel);
        s.start = bfr.text.length;
        s.end = s.start + sel.text.length;
    }
    return s;
}
Run Code Online (Sandbox Code Playgroud)

笔记:

  • sel必须通过target而不是使用返回的范围来创建 范围document.selection,否则bfr.setEndPoint将抱怨无效的参数.这个"功能"(在IE8中发现)似乎没有在规范中记录.
  • target 必须有输入焦点才能使此功能起作用.
  • 只有测试<textarea>,可能与工作<input>为好.