如何获取文本区域中的选择的开始和结束点? [英] How to get the start and end points of selection in text area?

查看:143
本文介绍了如何获取文本区域中的选择的开始和结束点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在文本字段或文本区域中获取所选范围的光标开始和结束位置。我试过很多功能在各种论坛。但是当选择的最后一个字符是一个新的行字符JavaScript忽略它在IE6。

解决方案

修订答案,2010年9月5日 b

要考虑到尾随换行符在IE中是棘手的,我没有看到任何解决方案。但是,有可能。以下是我之前在这里发布的一个新版本。



请注意,textarea必须有焦点,这个函数在IE中正常工作。如果有疑问,先调用textarea的 focus()方法。

  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\\\
/ g,\\\
);

//创建一个只在输入中存在的工作TextRange
textInputRange = el.createTextRange();
textInputRange.moveToBookmark(range.getBookmark());

//检查选择的开始和结束是否在输入的最后
//,因为moveStart / moveEnd不返回我们想要的结果
/ /在这些情况下
endRange = el.createTextRange();
endRange.collapsed(false);

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

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

return {
start:start,
end:end
} ;
}


i want to get the cursor start and end position of a selected range in a text-field or text-area. i tried lot of functions in various forums. but when the last character of the selection is a new line character JavaScript ignore it in IE6. any one having idea ?

解决方案

Revised answer, 5 September 2010

To account for trailing line breaks is tricky in IE, and I haven't seen any solution that does this. It is possible, however. The following is a new version of what I previously posted here.

Note that the textarea must have focus for this function to work properly in IE. If in doubt, call the textarea's focus() method first.

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

这篇关于如何获取文本区域中的选择的开始和结束点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆