您的位置:首页 > 其它

Firefox中元素获取焦点函数focus不起作用的解决方法

2016-09-07 17:56 531 查看
文章摘抄 http://openwares.net/firefox/firefox_element_focus_not_work.html

 

元素获取焦点函数focus()在IE中正常Firefox中却不起作用。
js校验输入框的函数

function is_number(feild) {
var strRegExp = /^\d+(\.\d{1,2})?$/;
if (!strRegExp.test(feild.value)) {
alert("请输入有效的数字,小数点后最多只能输入两位!");
feild.focus();
return false;
}
}

 

 
在IE中可以正常工作,在Firefox/(windows or linux)上却无法重新获取焦点。
有两种解决办法:
1、让元素先失去焦点再获取焦点
...
feild.blur();
feild.focus();
...
这种方法在Firefox/windows上行为是正常的,但在firefox/linux平台上仍然无法获取焦点
 
2、定时器
...
setTimeout(function(){feild.focus();},0);
...
这种方法在firefox/windows和firefox/linux平台上都可以正常工作。
 

 例子

<script>
function init(){
document.getElementById("inputId").focus();
}
</script>

<body onload="document.getElementById('test').focus()">
我要获取焦点:<input type="text" name="test" id="test">
</body>

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐