jon*_*bbs 161 javascript textbox input
有人知道如何将文本框中的键盘插入符移动到特定位置吗?
例如,如果一个文本框(例如输入元素,而不是文本区域)中有50个字符,并且我想在插入字符20之前放置插入符号,那么我该如何处理呢?
这与这个问题不同:jQuery在文本区域设置光标位置,这需要jQuery.
kd7*_*kd7 186
摘录自Josh Stodola 使用Javascript在Textbox或TextArea中设置键盘插入位置
一个通用函数,允许您将插入符号插入到您希望的文本框或文本区域的任何位置:
function setCaretPosition(elemId, caretPos) {
var elem = document.getElementById(elemId);
if(elem != null) {
if(elem.createTextRange) {
var range = elem.createTextRange();
range.move('character', caretPos);
range.select();
}
else {
if(elem.selectionStart) {
elem.focus();
elem.setSelectionRange(caretPos, caretPos);
}
else
elem.focus();
}
}
}
Run Code Online (Sandbox Code Playgroud)
第一个预期参数是您希望插入键盘插入符号的元素的ID.如果无法找到该元素,则不会发生任何事情(显然).第二个参数是插入符号位置索引.零将键盘插入开头.如果传递的数字大于元素值中的字符数,则会将键盘插入符号放在最后.
在IE6及更高版本,Firefox 2,Opera 8,Netscape 9,SeaMonkey和Safari上进行了测试.不幸的是,它在Safari上无法与onfocus事件结合使用).
使用上述函数强制键盘插入符号在获得焦点时跳转到页面上所有textareas的末尾的示例:
function addLoadEvent(func) {
if(typeof window.onload != 'function') {
window.onload = func;
}
else {
if(func) {
var oldLoad = window.onload;
window.onload = function() {
if(oldLoad)
oldLoad();
func();
}
}
}
}
// The setCaretPosition function belongs right here!
function setTextAreasOnFocus() {
/***
* This function will force the keyboard caret to be positioned
* at the end of all textareas when they receive focus.
*/
var textAreas = document.getElementsByTagName('textarea');
for(var i = 0; i < textAreas.length; i++) {
textAreas[i].onfocus = function() {
setCaretPosition(this.id, this.value.length);
}
}
textAreas = null;
}
addLoadEvent(setTextAreasOnFocus);
Run Code Online (Sandbox Code Playgroud)
小智 50
答案中的链接被破坏了,这个应该可以工作(所有学分都转到blog.vishalon.net):
http://snipplr.com/view/5144/getset-cursor-in-html-textarea/
如果代码再次丢失,这里有两个主要功能:
function doGetCaretPosition(ctrl)
{
var CaretPos = 0;
if (ctrl.selectionStart || ctrl.selectionStart == 0)
{// Standard.
CaretPos = ctrl.selectionStart;
}
else if (document.selection)
{// Legacy IE
ctrl.focus ();
var Sel = document.selection.createRange ();
Sel.moveStart ('character', -ctrl.value.length);
CaretPos = Sel.text.length;
}
return (CaretPos);
}
function setCaretPosition(ctrl,pos)
{
if (ctrl.setSelectionRange)
{
ctrl.focus();
ctrl.setSelectionRange(pos,pos);
}
else if (ctrl.createTextRange)
{
var range = ctrl.createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
}
Run Code Online (Sandbox Code Playgroud)
Mar*_*.io 33
因为我实际上真的需要这个解决方案,并且典型的基线解决方案(关注输入 - 然后设置值等于它自己)不能跨浏览器工作,我花了一些时间调整和编辑所有内容以使其工作.以@ kd7的代码为基础,这就是我想出的.
请享用!适用于IE6 +,Firefox,Chrome,Safari,Opera
// ** USEAGE ** (returns a boolean true/false if it worked or not)
// Parameters ( Id_of_element, caretPosition_you_want)
setCaretPosition('IDHERE', 10); // example
Run Code Online (Sandbox Code Playgroud)
肉和土豆基本上是@ kd7的setCaretPosition,最大的调整是if (el.selectionStart || el.selectionStart === 0)
,在firefox中,selectionStart从0开始,布尔当然转向False,所以它在那里打破.
在chrome中,最大的问题是仅仅给它它.focus()
是不够的(它一直选择所有文本!)因此,我们el.value = el.value;
在调用函数之前设置自身的值,现在它有一个掌握和位置输入以使用selectionStart.
function setCaretPosition(elemId, caretPos) {
var el = document.getElementById(elemId);
el.value = el.value;
// ^ this is used to not only get "focus", but
// to make sure we don't have it everything -selected-
// (it causes an issue in chrome, and having it doesn't hurt any other browser)
if (el !== null) {
if (el.createTextRange) {
var range = el.createTextRange();
range.move('character', caretPos);
range.select();
return true;
}
else {
// (el.selectionStart === 0 added for Firefox bug)
if (el.selectionStart || el.selectionStart === 0) {
el.focus();
el.setSelectionRange(caretPos, caretPos);
return true;
}
else { // fail city, fortunately this never happens (as far as I've tested) :)
el.focus();
return false;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
Joh*_*ald 21
我已经调整了kd7的答案,因为当selectionStart偶然为0时elem.selectionStart将评估为false.
function setCaretPosition(elem, caretPos) {
var range;
if (elem.createTextRange) {
range = elem.createTextRange();
range.move('character', caretPos);
range.select();
} else {
elem.focus();
if (elem.selectionStart !== undefined) {
elem.setSelectionRange(caretPos, caretPos);
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 13
我找到了一个简单的方法来解决这个问题,在 IE 和 Chrome 中进行了测试:
function setCaret(elemId, caret)
{
var elem = document.getElementById(elemId);
elem.setSelectionRange(caret, caret);
}
Run Code Online (Sandbox Code Playgroud)
将文本框 ID 和插入符号位置传递给此函数。