How to make input autofocus in internet explorer?

前端 未结 5 879
南旧
南旧 2020-12-03 18:17

Please check out my jsfiddle

http://jsfiddle.net/WK2N3/1/

How can I make this on autofocus like it is for chrome, safari, opera and firefox for IE?

相关标签:
5条回答
  • 2020-12-03 19:04

    You will have to rely on javascript to do this, since html5 autofocus is not supported in IE. There is a good blog post about it here : http://www.html5tutorial.info/html5-autofocus.php

    Basically, you first check if the attribute is supported, and then use javascript to manually focus in said input using the focus() method if it is not.

    0 讨论(0)
  • 2020-12-03 19:10

    You can try this.

    setTimeout(function() { document.getElementById('myInput').focus(); }, 10);
    

    for more detail AutoFOcusIE

    0 讨论(0)
  • 2020-12-03 19:10

    autofocus in IE is is just

    <input type="text" id="searchbar" autofocus />
    

    not

    <input type="text" id="searchbar" autofocus="autofocus"/>
    

    See http://msdn.microsoft.com/en-us/library/windows/apps/hh441087(v=vs.85).aspx for more info.

    0 讨论(0)
  • 2020-12-03 19:12

    Usually I use jQuery:

    $('input').focus()
    

    Live example: http://jsfiddle.net/simply_simpy/eb4JJ/5/

    0 讨论(0)
  • 2020-12-03 19:17

    Here's a one-liner (well, one line of actual logic) that uses jQuery to make autofocus work in IE. It bails out if the focus is already set--in other words, in any HTML5-capable browser.

    $(function() {
      $('[autofocus]:not(:focus)').eq(0).focus();
    });
    

    I explained how it works in my blog. And here is an updated jsFiddle that works in IE.

    0 讨论(0)
提交回复
热议问题