判断是否为空

<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <title></title>
  <script type="text/javascript">
  
   function show(id,info){
    var el = document.getElementById("user").value;
    var a = document.getElementById(id+"Name");
    if(el==""){
     a.style.display="inline-block";
     a.innerHTML=info;
    }else{
     a.style.display="none";
    }
   }
   onblur="check('user','用户名不能为空')"
   function check(id,info){
    var el = document.getElementById("user").value;
    var a = document.getElementById(id+"Name");
    if(el==""){
     a.style.display="inline-block"; 
     a.innerHTML=info;
    }else{
     a.style.display="none";
    }
   }
   
  </script>
 </head>
 <body>
  <table  cellspacing="0" cellpadding="0">
   <tr>
    <td>用户名</td>
    <td>
     <input type="text" id="user" onfocus="show('user','用户名必填')" onblur="check('user','用户名不能为空')"/><span id="userName" ></span>
    </td>
   </tr>
   <tr>
    <td>密码</td>
    <td>
     <input type="password" id="password" onfocus="show('password','密码必填')" onblur="check('password','密码不能为空')"/><span id="passwordName" ></span>
    </td>
   </tr>
   <tr>
    <td>确认密码</td>
    <td>
     <input type="password" id="passwordAgain" onfocus="show('passwordAgain','密码必填')" onblur="check('passwordAgain','密码不能为空')"/><span id="passwordAgainName" ></span>
    </td>
   </tr>
  </table>
 </body>
</html>

原文地址:https://www.cnblogs.com/zdy-123/p/10455584.html