[英]Is it possible to programmatically detect the caret position within a <input type=text> element?
Assuming a regular <input type=text>
text-box with data in it. 假设一个常规的
<input type=text>
文本框,其中包含数据。
Is it possible to detect (via JavaScript) the position of the text-coursor inside that text-box? 是否可以检测(通过JavaScript)文本框内文本方块的位置?
I am able to detect an ARROW LEFT or ARROW RIGHT keydown event - but how to detect the cursor location? 我能够检测到ARROW LEFT或ARROW RIGHT keydown事件 - 但是如何检测光标位置?
Why I need this: 为什么我需要这个:
I have a dynamic text-box here: http://vidasp.net/tinydemos/dynamic-textbox.html 我在这里有一个动态文本框: http : //vidasp.net/tinydemos/dynamic-textbox.html
It works great, however there are two scenarios that I would like to fix: 它工作得很好,但我想修复两种情况:
(In both cases, the text-box must contain data for the effect to be observable.) (在这两种情况下,文本框都必须包含可观察效果的数据。)
I've done quite a lot of work on this. 我在这方面做了很多工作。 The following works in all major browsers (including IE 6) for text
<input>
s and <textarea>
s and will work in all situations, including when there are leading and trailing spaces (which is where many solutions, including a-tools, fall down). 以下适用于所有主要浏览器(包括IE 6)的文本
<input>
和<textarea>
并且适用于所有情况,包括有前导和尾随空格(这是许多解决方案,包括a-tools,摔倒)。 There's some background to this code in this question: IE's document.selection.createRange doesn't include leading or trailing blank lines 这个代码在这个问题中有一些背景知识: IE的document.selection.createRange不包括前导或尾随空白行
You can also get the following as part of a jQuery input/textarea selection plug-in I've written that is as yet undocumented: http://code.google.com/p/rangyinputs/ 您还可以将以下内容作为我编写的jQuery输入/ textarea选择插件的一部分,该插件尚未记录: http : //code.google.com/p/rangyinputs/
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
};
}
var el = document.getElementById("your_input");
var sel = getInputSelection(el);
alert(sel.start + ", " + sel.end);
yes it's possible. 是的,这是可能的。
It's even simpler if you use 如果你使用它甚至更简单
http://plugins.jquery.com/project/a-tools http://plugins.jquery.com/project/a-tools
Good Luck :) 祝好运 :)
edit: please note that the cursor is msot often reffered to as "caret", just FYI ;) 编辑:请注意光标是msot经常被称为“插入符号”,只是FYI;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.