<!doctype html> <html> <head> <meta charset="utf-8"> <style type="text/css"> .red{color: red;} .green{color: green;} .black{color:#000;} .blue{color:blue;} </style> <body> <form action="phptest.php" method="post" id="obb" onsubmit="return intt(‘oo‘);"> Name: <input type="text" name="name" pp="123" id=""/><span>名字是名字</span><br/> Age: <input type="text" name="age" value="" /><span>年龄是数字</span><br/> <input type="submit" name="submit"> <input type="button" value="sada" id="abc"/> </form> </body> <script type="text/javascript"> var qq,bb,reg,pattern; qq=document.getElementById(‘obb‘); bb=qq.getElementsByTagName(‘input‘); pattern=/^[0-9]*[1-9][0-9]*$/; reg=/^[\s]{0,}$|^[\w\s]{7,}$/g; window.onload=function(){ intt(); document.getElementById("abc").onclick=function(){alert(intt())} } function check(obj,info,fun,clicks,yes_no){ obj.onfocus=function(){ obj.nextSibling.innerHTML=info; obj.nextSibling.className="blue"; } obj.onblur=function(){ if(fun()){ obj.nextSibling.innerHTML=info; obj.nextSibling.className="red"; yes_no.a = false; //重写传入参数的话改的是参数副本不会影响外部的值的, //如:yes_no=flase;这样是不会修改外面传进来的yes_no1和yes_no2的值的; //所以不能重写传入的参数,但是可以改变参数的属性; //传入基本函数类型只是传如值,传入引用类型的话就是传入变量引用副本地址, //传入后引用函数的副本地址是指向原来外部的引用地址的,如果重写就切断了和外面引用地址的联系, //而引用副本地址会变成新的引用地址,所以和外部没有关系) } else{ obj.nextSibling.innerHTML="正确"; obj.nextSibling.className="green"; yes_no.a = true; } } if (clicks==‘oo‘) { obj.onblur(); }; } var yes_no1={},yes_no2={}; function intt(clicks){ check(bb[0],"输入点名字", function(){ var qq =reg.test(bb[0].value); return qq; },clicks,yes_no1 ); check(bb[1],"输入点名字", function(){ var qq =reg.test(bb[1].value); return qq; },clicks,yes_no2 ); if(yes_no1.a&&yes_no2.a) {return true;} else {return false}; } </script> </html>