Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
820 views
in Technique[技术] by (71.8m points)

javascript - Autofocus doesn't work on Firefox and doesn't work when coming from another page

I'm creating my page using Javascript. It is a login page and I need to use autofocus on username input text. This operation works on IE and Chrome, but doesn't on Mozilla! This is the HTML I have been adding using JS:

'<input id="username" class="loginInput" data-bind="value: userName, valueUpdate: 'afterkeydown'" type="text" name="User" autofocus><!--User Input text end /-->'

When i load the page with Mozilla this input is set this way:

<input id="username" class="loginInput" type="text" autofocus="" name="User" data-bind="value: userName, valueUpdate: 'afterkeydown'">

I really can't understand why autofocus attribute is set in that way. I even tried by adding the attribute using JQuery $("#username").attr('autofocus','autofocus') and works only on Chrome and IE.

The second problem comes when I go to the login page from another page and autofocus issue comes with every browser.

Can you help me?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can try with:

$("#username").focus();

after the page has been loaded.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...