扫码关注官方订阅号
小伙看你根骨奇佳,潜力无限,来学PHP伐。
button变量没有声明啊~
<!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <script> function fun(){ var Ol=document.getElementById("text-name"); var Oin=document.getElementById("hello"); var Oform=document.getElementById("ceshi"); if(Ol.value.trim()==""){ var label=document.createElement("label") label.setAttribute("for","text-name"); Oform.insertBefore(label,Oin); Ol.labels[1].innerHTML="请输入您的姓名"; Ol.labels[1].setAttribute("style","font-size:9px;","color","blue") } } </script> </head> <body> <form id="ceshi"> <label id="nihao" for="text-name">姓名:</label> <input id="text-name"/> <input type="button" id="hello" value="验证" onClick="fun()"> </form> </body> </html>
写在onload可以
<!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <script> window.onload=function(){ var Ol=document.getElementById("text-name"); var Oin=document.getElementById("hello"); var Oform=document.getElementById("ceshi"); function fun(){ if(Ol.value.trim()==""){ var label=document.createElement("label") label.setAttribute("for","text-name"); Oform.insertBefore(label,Oin); Ol.labels[1].innerHTML="请输入您的姓名"; Ol.labels[1].setAttribute("style","font-size:9px;","color","blue") } } Oin.addEventListener("click",fun,this); } </script> </head> <body> <form id="ceshi"> <label id="nihao" for="text-name">姓名:</label> <input id="text-name"/> <input type="button" id="hello" value="验证" > </form> </body> </html>
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
button变量没有声明啊~
写在onload可以