没有把焦点设置到Firefox中的文本字段 [英] not setting focus to text field in firefox

查看:102
本文介绍了没有把焦点设置到Firefox中的文本字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个非常有趣的问题。
我试图设置焦点使用javascript(没有jQuery的,我试过,但也没有工作)的输入字段使用 window.onLoad



只要看看这个fiddel: setFocusOnLoad



它在Chrome浏览器中工作正常,但不在Firefox中。有没有在Firefox的问题。我该如何解决。感谢。

编辑:
这是我在html文件中复制的代码。

 <!DOCTYPE html> 
< html>
< head>
< title>< / title>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8>
< script type =text / javascript>
函数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>


解决方案

尝试添加轻微延迟:

 函数onloadFocus(){
setTimeout(function(){
document.getElementById('name')。focus()
},10);
}

更新jsFiddle


I faced a very interesting issue. I'm trying to set the focus on a input field using javascript ( no jquery, I tried that also but not worked) using window.onLoad.

just take a look at this fiddel : setFocusOnLoad

It's working fine in chrome browser but not in firefox. is there any Issue in firefox. how can i resolve. Thanks.

Editted: Here is the code I copied in html file.

<!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>

解决方案

Try adding a slight delay:

function onloadFocus(){
    setTimeout(function() {
        document.getElementById('name').focus()
    }, 10);
}

Update jsFiddle

这篇关于没有把焦点设置到Firefox中的文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆