没有将焦点设置为firefox中的文本字段

Rav*_*avi 2 html javascript javascript-events

我遇到了一个非常有趣的问题.我正在尝试使用javascript 设置焦点在输入字段上(没有jquery,我尝试了但也没用)window.onLoad.

看看这个fiddel: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>
Run Code Online (Sandbox Code Playgroud)

Pau*_*ann 6

尝试添加一点延迟:

function onloadFocus(){
    setTimeout(function() {
        document.getElementById('name').focus()
    }, 10);
}
Run Code Online (Sandbox Code Playgroud)

更新jsFiddle