我一直在使用 Android 版 Kik 网络应用程序。我注意到某些事件在用户与页面交互之前无法正确触发。
该问题在 Kik 中显现出来,但在我运行 Android 4.4/KitKat 的 Galaxy S5 上的内置网络浏览器中也出现了。
HTML
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
click here: <button id="button">a button</button>
<br><br><br>
textbox: <input type="text" id="textbox">
CSS
* {
font-size: 20px;
}
JavaScript
window.onload=function(e) {
// set function to button.click
$('#button').click(function() {
fucus();
});
// run the exact same function rightey-right now
$('#textbox').focus();
// run it again in three minutes
$('#textbox').focus()
};
function fucus() {
$('#textbox').focus();
}
最佳答案
大多数移动浏览器不允许 .focus()
按预期运行,除非从用户交互事件调用。例如,如果您在 onclick
事件中调用它,它将起作用,但如果在 onload
中调用它,则它将不起作用。这并非特定于 Kik,而是一般的移动浏览器。
关于javascript - 为什么用户必须在事件(如焦点等)工作之前点击我的应用程序内部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26825115/