简体   繁体   中英

How to set focus on first input field using jQuery in IE?

I can set focus on first input, that works in all browsers(except IE):

  $('#login').on('shown', function () {
  $(login).find('input:visible:first').focus();
})

I need to call it after Bootstrap modal showing will be finished, so I'm calling it in shown function.

Also tried this code(not working):

 $('#sign_up').on('shown', function () {
    setTimeout(function () {
    $(sign_up).find('input:visible:first').focus();
  }, 100);
 ///working everywhere except explorer
$('#login').on('shown', function () {
  $('#user_email').focus();
})

javascript

<script type="text/javascript">
function formfocus() {
  document.getElementById('element').focus();
}
window.onload = formfocus;
</script>

JAVASCRIPT DEMO

jquery

$(document).ready(function(){
  $('#element').focus();
});

JQUERY DEMO

JQUERY FOR IE 8

$(document).ready(function(){
  setTimeout(function() {
     $('#element').focus();
  }, 10);
});

IE 8 DEMO

HTML

<form>
  <input id="element" />
  <input />
  <input />
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM