ページロード時に特定の入力ボックスにカーソルを合わせるにはどうすればよいですか?フォーカス入力ボックスオンロード
初期テキスト値も保持し、入力の最後にカーソルを置くことは可能ですか?
<input type="text" size="25" id="myinputbox" class="input-text" name="input2" value = "initial text" />
ページロード時に特定の入力ボックスにカーソルを合わせるにはどうすればよいですか?フォーカス入力ボックスオンロード
初期テキスト値も保持し、入力の最後にカーソルを置くことは可能ですか?
<input type="text" size="25" id="myinputbox" class="input-text" name="input2" value = "initial text" />
質問には2つの部分があります。
1)ページの読み込みにどのように入力を集中させるのですか?
入力にはautofocus
属性を追加できます。
<input id="myinputbox" type="text" autofocus>
ただし、これは一部のブラウザでサポートされていない可能性がありますので、javascriptを使用することができます。
window.onload = function() {
var input = document.getElementById("myinputbox").focus();
}
2)どのように入力されたテキストの末尾にカーソルを置きますか?
ここには、一部の借用コードがanother SO answerの非jQueryソリューションがあります。
function placeCursorAtEnd() {
if (this.setSelectionRange) {
// Double the length because Opera is inconsistent about
// whether a carriage return is one character or two.
var len = this.value.length * 2;
this.setSelectionRange(len, len);
} else {
// This might work for browsers without setSelectionRange support.
this.value = this.value;
}
if (this.nodeName === "TEXTAREA") {
// This will scroll a textarea to the bottom if needed
this.scrollTop = 999999;
}
};
window.onload = function() {
var input = document.getElementById("myinputbox");
if (obj.addEventListener) {
obj.addEventListener("focus", placeCursorAtEnd, false);
} else if (obj.attachEvent) {
obj.attachEvent('onfocus', placeCursorAtEnd);
}
input.focus();
}
これは、jQueryでこれをどのように達成するかの例です。
<input type="text" autofocus>
<script>
$(function() {
$("[autofocus]").on("focus", function() {
if (this.setSelectionRange) {
var len = this.value.length * 2;
this.setSelectionRange(len, len);
} else {
this.value = this.value;
}
this.scrollTop = 999999;
}).focus();
});
</script>
コードスニペット:これを行うの
function focusOnMyInputBox(){
document.getElementById("myinputbox").focus();
}
<body onLoad="focusOnMyInputBox()")>
<input type="text" size="25" id="myinputbox" class="input-text" name="input2" onfocus="this.value = this.value;" value = "initial text" />
ポータブルな方法this oneのような(ブラウザの違いを処理するために)カスタム関数を使用しています。その後
あなた<body>
タグ、jessegavinが書いたようにの最後にセットアップonload
のハンドラを:
window.onload = function() {
document.getElementById("myinputbox").focus();
}
ジャストヘッドアップ - あなたは今、それをサポートするブラウザ用のJavaScriptをせずにHTML5でこれを行うことができます。
<input type="text" autofocus>
あなたは、おそらくこれで開始し、古いブラウザ用のフォールバックを提供するために、JavaScriptでその上に構築したいです。
これは、入力フィールドの最後の位置にカーソルを移動していることを知っていますか? –
@ Codex73がHTML5のプレースホルダ属性の機能を達成しようとしているとは思いません。現在入力されている値の末尾にカーソルが自動的に配置されるように思えます。 – jessegavin
そうです、これは完全な解決策ではありません。しかし、これは2つのことを解決します(onloadオートフォーカスとプレースホルダテキスト)。 –
$(document).ready(function() {
$('#id').focus();
});
これにはjQueryが必要です。 –
ええ、あなたはjQueryのlibsを含める必要があります:) – Nasruddin
これは私のために正常に動作するものである:
<form name="f" action="/search">
<input name="q" onfocus="fff=1" />
</form>
FFFは、名前は絶対に無関係であるとその狙いは、その入力にフォーカスを強制するために、一般的なonloadイベントを停止するようになり、グローバル変数になります。
<body onload="if(!this.fff)document.f.q.focus();">
<!-- ... the rest of the page ... -->
</body>
から:http://webreflection.blogspot.com.br/2009/06/inputfocus-something-really-annoying.html
あなたがフォームの後にこのを追加することができ、何らかの理由BODYタグに追加できない場合:
<SCRIPT type="text/javascript">
document.yourFormName.yourFieldName.focus();
</SCRIPT>
は正常に動作...
window.onload = function() {
var input = document.getElementById("myinputbox").focus();
}
試してみてください。
Javascriptのピュア:
[elem][n].style.visibility='visible';
[elem][n].focus();
のjQuery:
[elem].filter(':visible').focus();
最初のコードブロックは、実際にナンプラーを提案既存の値の末尾にカーソルを置くと、うまくいきます。私はあなたの助けを感謝します。 – Codex73