この次のセクションに進みたいと思います。わかりやすくするために、「Here!」というテキストを追加しました。デモでそれが最後まで行くことをあなたに示すために。
以下のデモでは、Enter keyから.field .value
を押して最後に移動します。
デモ:http: //jsfiddle.net/GLzKy/1/
以下は、実際にすべての作業を行うhttps://stackoverflow.com/a/4238971/297641の関数です。
$('.field .value').keydown(function (e) {
if (e.which == 13) { //Enter Key
e.preventDefault();
placeCaretAtEnd($(this).closest('.parent')[0]);
}
});
/**
This below function is from https://stackoverflow.com/a/4238971/297641
All credits goes to the original author.
*/
function placeCaretAtEnd(el) {
el.focus();
if (typeof window.getSelection != "undefined"
&& typeof document.createRange != "undefined") {
var range = document.createRange();
range.selectNodeContents(el);
range.collapse(false);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
} else if (typeof document.body.createTextRange != "undefined") {
var textRange = document.body.createTextRange();
textRange.moveToElementText(el);
textRange.collapse(false);
textRange.select();
}
}
FF、IE8、Chromeでテスト済み
参照: https ://stackoverflow.com/a/4238971/297641