如何在 Internet Explorer 7 中使用 jQuery 获取 textarea 中选定的文本?

发布于 2024-12-01 02:40:35 字数 546 浏览 2 评论 0原文

我尝试使用 jquery-fieldselection 插件 来获取文本区域中选定的文本。

它在 Firefox 和 Chrome 中工作正常,但在 Internet Explorer 7 中则不然。

我使用 getSelection() 方法,如下所示:

textarea.getSelection();

当文本区域中的文本为 12345 时,并且所有选择此文本,Firefox 和 Chrome 返回:

start: 0    // Correct!
end: 5

而 Internet Explorer 7 返回:

start: 5    // Why ??
end: 5

我正在寻找使用 jQuery 的跨浏览器解决方案。

I tried the jquery-fieldselection plugin to get the selected text in textarea.

It works fine in Firefox and Chrome, but not in Internet Explorer 7.

I use the getSelection() method like this:

textarea.getSelection();

When the text within the textarea is 12345, and all this text is selected, Firefox and Chrome returns:

start: 0    // Correct!
end: 5

while Internet Explorer 7 returns:

start: 5    // Why ??
end: 5

I'm looking for a cross browser solution using jQuery.

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

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

发布评论

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

评论(2

浅浅 2024-12-08 02:40:35

刚刚看了一下库,它对于 IE 的行为有所不同,因为它不支持现代浏览器所做的某些方法..可能是那里的代码不完美..

使用以下方法:

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

如何使用它:

你需要 dom 对象的文本区域..因此:

var textArea=    $("textarea")[0] or getElementbyId("textareaid");

var selectedText=getInputSelection(textArea);

var start=selectedText.start;
var end=selectedText.end;

现场演示

just took a look a tthelibrary and it behaves differently for IE since it does not support some methods that the modern browsers do.. may be the code there isnt perfect..

use the following method:

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

how to use it:

you need the dom object of the textarea.. thus:

var textArea=    $("textarea")[0] or getElementbyId("textareaid");

var selectedText=getInputSelection(textArea);

var start=selectedText.start;
var end=selectedText.end;

Live Demo

不忘初心 2024-12-08 02:40:35

这是一个非常简洁紧凑的 jquery 插件,适用于 IE7、IE8、IE9、FF、Chrome。选择的结束位置,还可以以编程方式替换所选文本。
工作示例/代码版本如下: http://jsfiddle.net/hYuzk/3/
带有更详细注释等的版本在这里: http://jsfiddle.net/hYuzk/4/

Here is a very concise and compact jquery plugin that works for IE7,IE8,IE9,FF,Chrome.. Allows you to get/set start & end position of a selection and also replace selected text programmatically.
A working example/code version is here: http://jsfiddle.net/hYuzk/3/
A version with more detailed comments etc. is here: http://jsfiddle.net/hYuzk/4/

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