依舊會用到表單元素javascript
和p標籤提示主要是用到css
p.className = "a a1";給元素添加類名的屬性:html
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <style type="text/css"> div{ width: 600px; margin:90px auto; } .a{ display:inline-block; font-size: 12px; color: #999; background: url(img/b.png) no-repeat left center; background-size: contain; padding-left: 20px; } .a1{ color: red; background-image: url(img/k.png); } .a3{ color: green; background: url(img/b.png); } </style> <title>Document</title> </head> <body> <div class=""> <input type="password" name="" id="" value="" class="a2"/> <p class="a">請輸出六至十六位密碼</p> </div> <script type="text/javascript"> var ipt = document.querySelector("input"); var p = document.querySelector("p"); ipt.onblur = function(){ if(ipt.value.length > 16 || ipt.value.length < 6){ p.className = "a a1"; p.innerHTML = "請輸入正確密碼" } else{ p.className = "a a2"; p.innerHTML = "格式正確" } } </script> </body> </html>