document.getElementById(id).focus()不适用于firefox或chrome [英] document.getElementById(id).focus() is not working for firefox or chrome
问题描述
当我做onchange事件时,它进入该函数的验证,但焦点不在于我正在使用 document.getElementById('controlid')。focus();
When ever I do onchange event, its going inside that function its validating, But focus is not comming I am using document.getElementById('controlid').focus();
我使用的是Mozilla Firefox和谷歌Chrome,两者都无效。我不想要任何IE浏览器。任何人都可以告诉我我的原因是什么。
I am using Mozilla Firefox and Google Chrome, in both its not working. I don't want any IE. Can any one tell me what could me the reason.
提前致谢
以下是代码:
var mnumber = document.getElementById('mobileno').value;
if(mnumber.length >=10) {
alert("Mobile Number Should be in 10 digits only");
document.getElementById('mobileno').value = "";
document.getElementById('mobileno').focus();
return false;
}
推荐答案
尝试使用计时器:
var mnumber = document.getElementById('mobileno').value;
if (mnumber.length >=10)
{
alert("Mobile Number Should be in 10 digits only");
document.getElementById('mobileno').value = "";
window.setTimeout(function ()
{
document.getElementById('mobileno').focus();
}, 0);
return false;
}
当线程空闲时,将运行计数为0的计时器。如果这没有帮助,请尝试在onblur事件中使用代码(带有计时器)。
A timer with a count of 0 will run when the thread becomes idle. If that doesn't help, try the code (with the timer) in the onblur event instead.
这篇关于document.getElementById(id).focus()不适用于firefox或chrome的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!