0

私は準備ができている関数の一番下にこれを持っています:

$("inputText").focus();

...そして、ページの読み込み中、カーソルしばらくの間 inputText 入力テキストにありますが、その後再びフォーカスが失われます。これを引き起こしている可能性があるのは何ですか?どうすればフォーカスをそこにとどめることができますか?

アップデート

「生の」Javascriptが違いを生むかどうか疑問に思いました:

document.getElementById("inputText").focus();

そうではありません。

おそらくこれは既知の問題です。アマゾンに行くと、カーソルが検索入力テキストにフォーカスを与えると思うかもしれませんが、そうではありません - 私はいつもクリックしなければなりません。それでも、それは Bing で機能するため、明らかに可能です。

4

2 に答える 2

1

ウィンドウ ロードを使用してみる

$(window).load(function () {

// コードを実行します });

ページ全体が読み込まれ、コードが実行され、入力フィールドにフォーカスが置かれることを意味します。ドキュメントへのリンクは次のとおりです: http://api.jquery.com/load-event/

于 2013-05-25T14:43:13.967 に答える
1

Is that your real code? Is your selector really inputText? In that case, that's probably the problem.

My guess is that the input is focused automatically first. Then the document ready handler kicks in and looks for a a tag inputText which I suppose you don't have. I guess you have <input id="inputText" />, which is selected with $("#inputText") so the selector doesn't find anything and moves the focus away from the input.

An additional check since you've tagged this asp.net. Are you using web forms? In that case html ids are not reliable since the web forms engine mess upp the ids - use a css class intead and a $(".inputText") selector.

于 2013-05-25T14:42:56.497 に答える