我遇到了一个非常有趣的问题。
我正在尝试使用 window.onLoad
使用 Javascript(没有 jQuery,我也尝试过但没有成功)将 焦点 设置在输入字段上。
看看这个 fiddle :setFocusOnLoad
它在 chrome 浏览器中运行良好,但在 Firefox 中运行不正常。 Firefox 有什么问题吗?我该如何解决。
已编辑:
这是我在 html 文件中复制的代码:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function onloadFocus(){
var name = document.getElementById('name');
// name.value='window.onload called';
name.focus();
}
window.onload=onloadFocus();
</script>
</head>
<body>
<div><input type='text' value='' id='name' name='name'></div>
</body>
</html>
最佳答案
尝试稍微延迟一下:
function onloadFocus(){
setTimeout(function() {
document.getElementById('name').focus()
}, 10);
}
关于javascript - 不将焦点设置到 Firefox 中的文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12542897/