<head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> div { width: 600px; margin: 100px auto; } .message { display: inline-block; font-size: 16px; color: #999; background: url(images/mess.png) no-repeat left center; padding-left: 20px; background-size: 20px; } .wrong { color: red; background: url(images/wrong.png) no-repeat left center; background-size: 20px; } .right { color: green; background: url(images/right.png) no-repeat left center; background-size: 20px; } </style> </head> <body> <div class="register"> <input type="password" class="ipt"> <p class="message">请输入6-16位密码</p> <script> // 事件源 var ipt = document.querySelector('.ipt') var message = document.querySelector('.message') // 注册事件失去焦点 ipt.onblur = function () { if (ipt.value.length < 6 || ipt.value.length > 16) { message.innerHTML = '请输入6-16位密码' message.className = 'message wrong' } else { message.className = 'message right' message.innerHTML = '输入正确' } } </script> </div> </body>
原创文章,作者:bd101bd101,如若转载,请注明出处:https://blog.ytso.com/278133.html