JOE个人网站

JOE个人网站,不仅仅是一个网站,更像是一个展现自我的平台,致力于让朋友们都可以
有所感触,有所收获。

onfocus_onblur

2017-12-19 15:58:20
-----------------------onreset、用户名密码不能为空、onfocus、onblur--------
onfocus - 获取焦点事件
onblur - 失去焦点事件
--------------------------------------------------------
  <script type="text/javascript" charset="utf-8">
   window.onload=function() {
    oinput=document.getElementsByName('like[]');
   }
   function change() {
    var xx=oinput[oinput.length-1].checked;
    for(var i=0;i<oinput.length-1;i++) {
     if(xx==true) {
      oinput[i].checked=true;
     } else {
      oinput[i].checked=false;
     }
    }
   }
   function check() {
    onames=document.getElementsByName('username');
    opwds=document.getElementsByName('password');
   // var olikes=document.getElementByName('like[]');
    var flag1=true;
    if(onames[0].value=='') {
     document.getElementById('username').innerHTML="用户名不能为空";
     flag1=false;
    }

    if(opwds[0].value=='') {
     document.getElementById('password').innerHTML="密码不能为空";
     flag1=false;
    }

    var flag2=false;
    for(var i=0;i<oinput.length-1;i++) {
     if(oinput[i].checked==true) { 
      flag2=true;
      break;
     }
    }

    if(flag1==true && flag2==true) {
     return true;
    } else {
     return false;
    }
   }

   function cl() {
    document.getElementById('username').innerHTML='';
    document.getElementById('password').innerHTML='';
   }

   function look() {
    if(opwds[0].value=='') {
     document.getElementById('password').innerHTML='密码不能为空';
    }

    if(onames[0].value=='') {
     document.getElementById('username').innerHTML='用户名不能为空';
    }
   }
  </script>
 </head>
 <body>

  <form action="./a.php" method="post" onsubmit='return check()' onreset='return confirm("确定要重置吗")'>
   姓名:<input type="text" name="username" onfocus='cl()' onblur='look()' /><span style="color:red;" id="username"></span><br/>
   密码:<input type="password" name="password" onfocus='cl()' onblur='look()' /><span style="color:red" id="password"></span><br/>
   爱好:
   <input type="checkbox" name="like[]" value="girl" />女孩
   <input type="checkbox" name="like[]" value="boy" />男孩
   <input type="checkbox" name="like[]" value="unkonw" />未知
   <br/>
   <input type="submit" name="submit" value="提交" />
   <input type="reset" name="reset" value="重置" />
   <br/>
   设置:
   <input type="checkbox" name="like[]" onclick='change()' />
  </form>
 </body>
------------------------------------------------
在JS中,中文和英文字母一样,一个字算一个长度
------------------------------------------------
  <script type="text/javascript" charset="utf-8">
   //JS中不分中英文,在计算字符串长度的时候
   var str='你df好';
   alert(str.length); //4
  </script>