测试和使用环境:
Google Chrome浏览器,手动触发焦点事件(如按键盘↑键)
代码如下:
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