87,968
社区成员
发帖
与我相关
我的任务
分享
<!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里 还没加载到元素呢