Html 如何在 Internet Explorer 中进行输入自动对焦?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8280988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make input autofocus in internet explorer?
提问by James
Please check out my jsfiddle
请查看我的 jsfiddle
How can I make this on autofocus like it is for chrome, safari, opera and firefox for IE?
我怎样才能像 IE 的 chrome、safari、opera 和 firefox 一样在自动对焦上做到这一点?
回答by Brian Morearty
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.
这是使用 jQuery 使自动对焦在 IE 中工作的单行(好吧,一行实际逻辑)。如果焦点已经设置——换句话说,在任何支持 HTML5 的浏览器中,它就会退出。
$(function() {
$('[autofocus]:not(:focus)').eq(0).focus();
});
I explained how it works in my blog. And hereis an updated jsFiddle that works in IE.
回答by Leo
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
您将不得不依赖 javascript 来执行此操作,因为 IE 不支持 html5 自动对焦。这里有一篇很好的博客文章: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.
基本上,您首先检查该属性是否受支持,然后使用 javascript 手动聚焦在所述输入中,focus()
如果不支持,则使用该方法。
回答by Scott Simpson
Usually I use jQuery:
通常我使用jQuery:
$('input').focus()
Live example: http://jsfiddle.net/simply_simpy/eb4JJ/5/
回答by Singhak
You can try this.
你可以试试这个。
setTimeout(function() { document.getElementById('myInput').focus(); }, 10);
for more detail AutoFOcusIE
了解更多细节AutoFOcusIE
回答by Nick Zimmerman
autofocus in IE is is just
IE 中的自动对焦只是
<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).aspxfor more info.
有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/windows/apps/hh441087(v=vs.85).aspx。