优化默认密码输入框 Posted on 2016-06-19 | In Javascript | | 主要是为了弥补在移动端原生密码输入框默认样式比较单调,通过这样能够实现自定义密码框输出样式。代码如下,仅供参考— 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>优化默认密码框</title> <meta name="viewport" content="width=device-width,initial-scale=1.0,user-scaleable=0" /> <style> * { margin: 0; padding: 0; } html { height: 100%; font-size: 16px; } body { height: 100%; background-color: skyblue; } .container { position: relative; margin: 100px 100px; } .password { display: block; } ul { display: inline-block; border: 1px solid #fff; margin: 0 auto; font-size: 0; position: relative; width: 12rem; height: 2rem; left: 50%; transform: translateX(-50%); } li { display: inline-block; width: 2rem; height: 2rem; background-color: #fff; font-size: 1.2rem; font-weight: 700; text-align: center; line-height: 2rem; border-left: 1px solid #e6e6e6; box-sizing: border-box; vertical-align: middle; overflow: hidden; } li:first-child { border-left: 0 none } .input { position: absolute; top: 0; left: -100%; width: 200%; height: 100%; opacity: 0; text-indent: -9999px; } </style></head><body> <div class="container"> <div class="password"> <ul> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> </ul> </div> <input class="input" type="tel" maxlength="6" id="input"> <label for="input"></label> </div> <script src="./zepto.js"></script> <script> $('body').on('input', '.input', function() { var $this = $(this), $inputs = $this.parent().find('.password li'), password = $this.val() + ''; var numLen = 6; for (var i = 0; i < numLen; i++) { if (password[i]) { $($inputs[i]).text('●'); } else { $($inputs[i]).text(''); } } if (password.length == 6) { alert('6位密码已输完'); $this.val(''); $inputs.html(''); } }); </script></body></html> 点击查看效果