JavaScriptフォーカスを取得し、カーソルを末尾文字に移動


詳細
テストおよび使用環境:
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);
        }
    }

}

リファレンスhttp://jsfiddle.net/ghAB9/3/
キーボード・リスニング・コードの添付

        document.onkeydown = function(e){
            switch (e.which){
                case 9:     //tab

                    break;
                case 13:    //enter

                    break;
                case 38:    //up
                    
                    break;
                case 40:    //down
                    
                    break;
            }

        };