设有一个文本框,只允许输入数值。分别用fous、change、blur判断。求哪位大神看看,哪出错了?菜鸟级别

喵喵fr 2015-09-22 09:25:18
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" >
var EventUtil = {
addHandler : function(element,type,handler){
if(element.addEventListener){
element.addEventListener(type,handler,false);
}else if(element.attachEvent){
element.attachEvent("on" + type,handler);
}else{
element["on" + type] = handler;
}
},

getEvent : function(event){
return event ? event : window.event;
},
getTarget : function(event){
return event.target || event.srcElement;
}
};
var textbox = document.forms[0].elements[0];
EventUtil.addHandler(textbox,"focus",function(event){
event = EventUtil.getEvent(event);
var target = EventUtil.getTarget(event);
if(target.style.backgroundColor != "red"){
target.style.backgrounColor = "yellow";
}
});

EventUtil.addHandler(textbox,"blur",function(event){
event = EventUtil.getEvent(event);
var target = EventUtil.getTarget(event);
if(/[^\d]/.test(target.value)){
target.style.backgroundColor = "red";
}else{
target.style.backgroundColor = "";}
});

EventUtil.addHandler(textbox,"change",function(event){
event = EventUtil.getEvent(event);
var target = EventUtil.getTarget(event);
if(/[^\d]/.test(target.value)){
target.style.backgroundColor = "red";
}else{
target.style.backgroundColor = "";
}
});
</script>
</head>
<body>
<from method = "post" id = "myForm">
<input type="text" id="f1"/>
</from>
</body>
</html>
...全文
136 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
喵喵fr 2015-10-06
  • 打赏
  • 举报
回复
恩恩 知道了 谢谢啦
似梦飞花 2015-09-22
  • 打赏
  • 举报
回复
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script type="text/javascript" >
        var EventUtil = {
            addHandler : function(element,type,handler){
                if(element.addEventListener){
                    element.addEventListener(type,handler,false);
                }else if(element.attachEvent){
                    element.attachEvent("on" + type,handler);
                }else{
                    element["on" + type] = handler;
                }
            },

            getEvent : function(event){
                return event ? event : window.event;
            },
            getTarget : function(event){
                return event.target || event.srcElement;
            }
        };
        window.onload=function(){
            var textbox = document.getElementById('f1');


            EventUtil.addHandler(textbox,"blur",function(event){
                event = EventUtil.getEvent(event);
                var target = EventUtil.getTarget(event);
                if(/[^\d]/.test(target.value)){
                    target.style.backgroundColor = "red";
                }else{
                    target.style.backgroundColor = "";}
            });

            
        }

    </script>
</head>
<body>
<from method = "post" id = "myForm">
    <input type="text" id="f1"/>
</from>
</body>
</html>
放在onload里 还没加载到元素呢

87,937

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧