<head>.red{border:1px solid red;}</head><body>姓名:<input type="text"><span></span></body><script type="text/javascript"> var name=document.getElementsByTagName("Input")[0]; var tip=document.getElementsByTagName("span")[0]; function onfocusFn(){ if(this.value==""){ tip.innerHTML="请输入您的姓名"; }else{ tip.value=""; } } function onblurFn(){ if(this.value==""){ tip.innerHTML="用户名不能为空"; this.style.className="red"; }else{ tip.value=""; this.removeAttribute("class"); } } name.onfocus=onfocusFn; name.onblur=onblurFn;</script>请指教哪里出错了,好烦啊。。。速速帮忙解答。
1 回答
码农2号
TA贡献151条经验 获得超48个赞
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> <style> .red{border:1px solid red;} </style> </head> <body> 姓名:<input type="text" id="inp"><span></span> <script type="text/javascript"> // var name=document.getElementsByTagName("input")[0]; var inp=document.getElementById('inp') var tip=document.getElementsByTagName("span")[0]; function onfocusFn(){ if(this.value==""){ tip.innerHTML="请输入您的姓名"; }else{ tip.innerHTML=""; } } function onblurFn(){ if(this.value==""){ tip.innerHTML="用户名不能为空"; this.classList.add('red'); }else{ tip.innerHTML=""; this.classList.remove('red'); } } inp.onfocus=onfocusFn; inp.onblur=onblurFn; </script> </body> </html>
添加回答
举报
0/150
提交
取消