简体   繁体   English

获取文本区域(IE)中的插入符位置

[英]Get caret position in textarea (IE)

I want to get the caret position inside text area (Internet Explorer only). 我想获得文本区域内的插入符位置(仅Internet Explorer)。 I have this: 我有这个:

document.activeElement.focus();             
var sel2 = document.selection.createRange();
sel2.moveStart('character', -document.activeElement.value.length);
var caretPos = sel2.text.length;
alert(caretPos);

If textarea is one line only I get correct result, but if there are many lines the result is wrong because new line takes extra char that is not visible... 如果textarea仅是一行,我会得到正确的结果,但是如果有很多行,则结果是错误的,因为新行会占用不可见的多余字符...

How can I get the correct position value (without removing \\r\\n and similar dirty work)? 如何获得正确的位置值(不删除\\ r \\ n和类似的脏工作)?

I would argue that counting line breaks as two characters when calculating selection boundaries/cursor position is the correct approach. 我认为在计算选择边界/光标位置时将换行符计数为两个字符是正确的方法。 Fundamentally, those two characters \\r\\n are there in the text. 根本上,这两个字符\\r\\n在文本中。 They are present in the textarea's value property and they are in the value that is submitted to the server. 它们存在于textarea的value属性中,并且存在于提交给服务器的值中。

The only arguments I can see for counting line breaks as one character are: 我可以看到的将换行符算作一个字符的唯一参数是:

  1. Consistency with other browsers 与其他浏览器的一致性
  2. IE's TextRange methods consider the line breaks as one character IE的TextRange方法将换行符视为一个字符

I think neither is valid. 我认为这都不成立。 Firstly, IE is already inconsistent with other browsers in counting line breaks as two characters rather than one. 首先,IE在将换行符计算为两个字符而不是一个字符时已经与其他浏览器不一致。 Secondly, IE's TextRange character-based methods are a little insane anyway, causing problems wherever they're used. 其次,IE的基于TextRange字符的方法无论如何还是有些疯狂,无论在何处使用都会引起问题。

I think it makes total sense to consider the selection/caret position as a position relative to the actual text in the textarea. 我认为将选择/插入位置视为相对于文本区域中实际文本的位置是完全有意义的。 This allows easy manipulation of the text. 这样可以轻松处理文本。

Here are two main functions. 这是两个主要功能。 The first is the only textarea selection/caret position getting function I've seen that works correctly with all line breaks. 第一个是我见过的唯一的textarea选择/插入符号位置获取功能,该功能可以在所有换行符中正常使用。 You can find this here: How to get the start and end points of selection in text area? 您可以在此处找到: 如何在文本区域中获得选择的起点和终点? . Second, here's a complementary setSelection function: 其次,这是一个补充的setSelection函数:

function offsetToRangeCharacterMove(el, offset) {
    return offset - (el.value.slice(0, offset).split("\r\n").length - 1);
}

function setSelection(el, startOffset, endOffset) {
    var range = el.createTextRange();
    var startCharMove = offsetToRangeCharacterMove(el, startOffset);
    range.collapse(true);
    if (startOffset == endOffset) {
        range.move("character", startCharMove);
    } else {
        range.moveEnd("character", offsetToRangeCharacterMove(el, endOffset));
        range.moveStart("character", startCharMove);
    }
    range.select();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用jQuery caret插件在IE中textarea中的插入符位置 - Caret position in textarea in IE using jQuery caret plugin 如何获得SCEditor文本区域中的当前插入符位置? - How to get current caret position in a SCEditor textarea? 改变textarea onclick的价值会使插入符号处于一个奇怪的位置,但仅限于IE - Changing value of textarea onclick puts caret in a weird position, but in IE only javascript FreeTextBox获取插入符位置(IE) - javascript FreeTextBox get caret position (IE) 在IE中使用jCaret插件获取插入符位置 - Get caret position using jCaret Plugin in IE 在可编辑div IE8中获得插入符位置 - Get caret position in editable div IE8 如何从一开始就以字符形式在textarea中获取插入符号列(不是像素)的位置? - How to get the caret column (not pixels) position in a textarea, in characters, from the start? Javascript:如何获取textarea中的line / col插入符号位置? - Javascript: how to get line/col caret position in textarea? Javascript获取输入文字或文本区域中插入符号的绝对位置(以像素为单位) - Javascript get the absolute position (in px) of a caret in a input text or textarea Tinymce - 是否有可能获得与所见即所得编辑器对应的textarea的插入位置? - Tinymce - Is it possible to get caret position of textarea corresponding to WYSIWYG Editor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM