测试和使用环境:
Google Chrome浏览器,手动触发焦点事件(如按键盘↑键)
代码如下:
参考 [url]http://jsfiddle.net/ghAB9/3/[/url]
附键盘监听代码
Google Chrome浏览器,手动触发焦点事件(如按键盘↑键)
代码如下:
function focusOn(_textarea, moveToLast){
this.moveToLastFlag = moveToLast;
function moveToTextEnd() {
if (this.moveToLastFlag){
if (typeof _textarea.selectionStart == "number") {
_textarea.selectionStart = _textarea.selectionEnd = _textarea.value.length;
} else if (typeof _textarea.createTextRange != "undefined") {
_textarea.focus();
var range = _textarea.createTextRange();
range.collapse(false);
range.select();
}
}
}
_textarea.focus();
_textarea.onfocus = function(e){
moveToTextEnd();
// Work around Chrome's little problem
if (window.chrome){
window.setTimeout(function() {
moveToTextEnd();
}, 1);
}
}
}
参考 [url]http://jsfiddle.net/ghAB9/3/[/url]
附键盘监听代码
document.onkeydown = function(e){
switch (e.which){
case 9: //tab
break;
case 13: //enter
break;
case 38: //up
break;
case 40: //down
break;
}
};
这篇博客介绍了如何在JavaScript环境下,特别是在Google Chrome浏览器中,实现对输入框的焦点获取,并将光标移动到文本的末尾。内容包括具体的代码示例和键盘监听代码。
3573

被折叠的 条评论
为什么被折叠?



