在 IE 中使用 jCaret 插件获取插入符位置

发布于 2024-09-26 03:25:56 字数 417 浏览 1 评论 0原文

我在 IE 中使用 jCaret 插件时遇到问题。

jCaret 下载

我想获取文本区域内的插入符号位置。

我的代码如下所示:

$('.myTextArea').click(function(){
    var myCaretPos = $(this).caret().end;
});

IE 中的问题是,如果文本区域只有一行,我只能得到正确的结果,但如果有多于一行,则结果是错误的。看来 IE 中换行符要多一个字符。那么如何去除IE中的换行符呢?

I have problem with jCaret plugin in IE.

jCaret downloads

I want to get the caret position inside a textarea.

My code looks like this:

$('.myTextArea').click(function(){
    var myCaretPos = $(this).caret().end;
});

The problem in IE is that I only get correct result if the textarea is only one line, but if there are more than one line the result is wrong. It seems that line breaks takes one extra char in IE. So how can I remove line breaks in IE?

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

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

发布评论

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

评论(1

情痴 2024-10-03 03:25:56

我遇到过并解决了这个问题。下面是一个在 jQuery 之外执行此操作的函数:

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
    };
}

以及如何使用它的示例:

$('.myTextArea').click(function(){
    var myCaretPos = getInputSelection(this).end;
});

我还制作了一个 jQuery 插件,其中包含此功能以及与文本区域选择相关的其他功能。它尚未发布,但已经稳定并经过测试。来源在这里:http://code .google.com/p/rangy/source/browse/trunk/src/js/modules/textinputs_jquery.js。例子:

$('.myTextArea').click(function(){
    var myCaretPos = $(this).getSelection().end;
});

I've encountered and solved this problem. Here's a function that will do it outside jQuery:

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
    };
}

And an example of how to use it:

$('.myTextArea').click(function(){
    var myCaretPos = getInputSelection(this).end;
});

I've also made a jQuery plug-in that includes this functionality, along with other functionality relating to textarea selections. It's not yet released but is stable and tested. Source is here: http://code.google.com/p/rangy/source/browse/trunk/src/js/modules/textinputs_jquery.js. Example:

$('.myTextArea').click(function(){
    var myCaretPos = $(this).getSelection().end;
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文