js在onblur方法里调用自己的focus方法,但无法获得焦点,js也没报错
<html><head><metahttp-equiv="Content-Type"content="text/html;charset=UTF-8"><title>In...
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript">
window.onload = function(){
var number = document.getElementById("number");
var pattern = /^[0-9]{6,16}$/;
number.onblur = function(){
if(pattern.test(number.value)){
number.style.backgroundColor = "white";
}
else{
number.style.backgroundColor = "pink";
number.focus();
}
}
}
</script>
<body>
<form action="#">
<input type="text" name="number" id="number" />
</form>
</body>
</html> 展开
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript">
window.onload = function(){
var number = document.getElementById("number");
var pattern = /^[0-9]{6,16}$/;
number.onblur = function(){
if(pattern.test(number.value)){
number.style.backgroundColor = "white";
}
else{
number.style.backgroundColor = "pink";
number.focus();
}
}
}
</script>
<body>
<form action="#">
<input type="text" name="number" id="number" />
</form>
</body>
</html> 展开
3个回答
展开全部
number.onblur = function (){this.focus()};
经检测,ie是支持这种写法的,chrome不支持
以下可行
var number = document.getElementById("number");
window.onload = function(){
var pattern = /^[0-9]{6,16}$/;
number.onblur = function (){setTimeout("number.focus()",0)};
}
展开全部
var number;
var pattern;
window.onload = function(){
number = document.getElementById("number");
pattern = /^[0-9]{6,16}$/;
}
number.onblur = function(){
if(pattern.test(number.value)){
number.style.backgroundColor = "white";
}
else{
number.style.backgroundColor = "pink";
number.focus();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你可以设置个hover属性,就会发现Hover属性会生效,但是光标无法定位。
我现在就是用setTimeout来处理的,过个一两遍毫秒后执行focus
我现在就是用setTimeout来处理的,过个一两遍毫秒后执行focus
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询