<script> var user = document.getElementById("user"); var pwd = document.getElementById("pwd"); var user_erro = document.getElementById("user_erro"); var pwd_erro = document.getElementById("pwd_erro"); //focus 获得焦点 user.addEventListener("blur", function () { check_length(user); //传递参数 }, false); pwd.addEventListener("blur", function () { check_length(pwd); }, false); function check_length(obj) { var that = obj; if (that.value.length < 1) { that.style.borderColor = 'rgb(255, 76, 82)'; //为空 show_dom(that.id,"block"); } else { that.style.borderColor = 'rgb(17, 194, 109)'; //不为空 show_dom(that.id,"none"); } } function show_dom(obj,disp) { switch (obj) { case "user": user_erro.style.display = disp; break; case "pwd": pwd_erro.style.display = disp; break; } } </script>