テキストフィールドにフォーカスを合わせる


■動作ブラウザ 【 IE:3.0  NN:2.0】
Internet Explorer Netscape Navigator DreamPassport iCab
3.0x 4.0x 4.5 5.0x 5.5 2.0x 3.0x 4.0x 4.x 6.0 2 3 2.x
Windows ○ ○ - ○ ○ ○ ○ ○ ○ ○ - ○ -
Macintosh ○ ○ ○ ○ - ○ ○ ○ ○ ○ - - ○
UNIX - - - - - ○ ○ ○ ○ ○ - - -
Dreamcast - - - - - - - - - - ○ ○ -

ポイント <body onLoad="document.myFORM.No2.focus()">
説  明 特定のテキストフィールドにフォーカスを合わせるにはfocus()を使います。「document.フォーム名.テキストフィールド名.focus()」とする事で任意のテキストフィールドにフォーカスを合わせることが出来ます。ページ読み込み後にフォーカスを合わせるためにbodyタグのonLoadイベントと組み合わせます。
サンプル <html> <head> <title>テキストフィールドにフォーカスを合わせる</title> </head> <body onLoad="document.myFORM.No2.focus()"> <form name="myFORM"> <input type="text" value="1番目" name="No1"><br> <input type="text" value="2番目" name="No2"><br> <input type="text" value="3番目" name="No3"><br> </form> </body> </html>
補足説明 フォーカスを外す場合はfocus()の代わりにblur()を使います。

■サンプルスクリプトを実行する >>実行
■各ブラウザでの動作結果を見る >>View!

写真素材 PIXTA