在学习了HTML DOM对象后,做几个小练习来巩固一下所学内容。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> 账号:<input type="text" id="account"><br> <div id="tipa"></div><br> <button onclick="judge()">判断</button> </body> <script> function judge() { var judgein = /^[a-zA-Z][a-zA-Z0-9_]{4,15}$/; var account = document.getElementById("account"); var tip2 = document.getElementById("tipa"); if(!judgein.test(account.value)) { var color = document.createAttribute("style"); color.nodeValue = "color: #ff1120"; tip2.setAttributeNode(color); tip2.innerHTML="错误"; } else { document.getElementById("tipa").innerHTML="正确"; var color = document.createAttribute("style"); color.nodeValue = "color: #00ff22"; tip2.setAttributeNode(color); } } </script> </html>