html中行内调用function的时候,是通过window调用的function,所以打印this等于打印window,所以在使用行内注册事件时务必传入参数this
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <style> .wrong { border: 2px solid red; } .right { border: 2px solid #91B81D; } </style> </head> <body> 账号:<input type="text" name="name" value="" onblur="fn(this)" /><br /><br /> 密码:<input type="text" name="name" value="" onblur="fn(this)"/> <script> function fn(ele) { if (ele.value.length < 6 || ele.value.length > 12) { ele.className = "wrong"; } else { ele.className = "right"; } } </script> </body> </html>