简体   繁体   English

移动Safari-自动对焦不起作用

[英]Mobile Safari - Autofocus not working

On mobile safari on iPhone... 在iPhone上的移动浏览器中...

Before I move on and mark this as unresolvable... is there anyway to autofocus an input field once the page loads? 在我继续并将其标记为无法解决之前,...页面加载后是否有自动聚焦输入字段的方法? I don't care when the autofocus gets triggered - I just want it to be automatic vs. clicking somewhere. 我不在乎自动对焦何时触发-我只希望它是自动的,而不是单击某个地方。 I've tried js, HTML5, and jQuery = all failed. 我已经尝试过js,HTML5和jQuery =全部失败。 I know there is 100+ post, from back when. 我知道从什么时候开始就有100多个帖子。 I just want to make sure there is no way to do this. 我只想确保没有办法做到这一点。

<input autofocus id="AutoFocusMe" type="text" placeholder="Email Address"/>

JS I've tried: 我尝试过的JS:

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

... ...

document.getElementById("AutoFocusMe").focus();

... ...

    if (!("autofocus" in document.createElement("input"))) 
{document.getElementById("AutoFocusMe").focus(); }

and

setTimeout(function() {
jQuery('#AutoFocusMe').focus();
}, 500);

Add this to the list now... 现在将此添加到列表...

document.getElementById('AutoFocusMe').click();

what about just checking if autofocus is undefined after the window is loaded, making sure all form elements are loaded. 只检查加载窗口后是否undefined autofocus ,确保所有表单元素都已加载。

My example runs in codepen debug mode (no iframe) 我的示例在Codepen调试模式下运行(无iframe)

http://s.codepen.io/jonathan/debug/xwgWKe ? http://s.codepen.io/jonathan/debug/xwgWKe

You need to run this code either on a remote server or local server, not through an iframe through JSfiddle or CodePen . 您需要在远程服务器或本地服务器上运行此代码,而不是通过JSfiddle或CodePen通过iframe运行此代码 JSfiddele and CodePen run code within an iframe. JSfiddele和CodePen在iframe中运行代码。 My example above runs in Codepen Debug mode. 我上面的示例在Codepen调试模式下运行。 So it doesn't run the code in an iframe. 因此,它不会在iframe中运行代码。 An iframe will block the autofocus iframe将阻止autofocus

// wait until all links, inputs, images are loaded
$(window).on("load", function(){

   if(document.createElement("input").autofocus === undefined) {
       $("[autofocus]").filter(":last").focus();
   }

});

if autofocus attribute is undefined you use an attribute selector to give focus to the last input with the autofocus attribute. 如果undefined autofocus属性,则使用属性选择器将焦点赋予具有autofocus属性的最后一个输入。

The reason i used jQuery on() load event handler, was to make sure that the event listener fires consistently for cross browser. 我使用jQuery on() load事件处理程序的原因是为了确保跨浏览器始终触发事件监听器。 Since the window.load doesn't work consistently cross browser. 由于window.load在跨浏览器中无法始终如一地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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