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

匆匆过客 提交于 2019-11-28 09:02:48

问题


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 secondo problem comes when i go to the login page from another page and autofocus issue comes with every browser.

Can you help me?


回答1:


You can try with:

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

after the page has been loaded.




回答2:


Instead of using the autofocus attribute, try:

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


来源:https://stackoverflow.com/questions/26364577/autofocus-doesnt-work-on-firefox-and-doesnt-work-when-coming-from-another-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!