/** * * 禁止浏览器自动填充密码 * * @method disabledRememberPassword * @param {any} el 目标(可多个) * */ function disabledRememberPassword(el) { var _el = typeof el === ‘object‘ ? el : $(el); if (!_el || _el.length == 0) return; _el.each(function (index, item) { $(item).attr(‘ilength‘, 0).attr(‘autocomplete‘, ‘off‘).attr(‘type‘, ‘text‘).attr(‘readonly‘, ‘readonly‘).val(‘‘).on(‘focus‘, function () { this.type != ‘password‘ ? this.type = ‘password‘ : 1; }).on(‘mouseout‘, function () { this.setAttribute(‘readonly‘, ‘readonly‘); }).on(‘mouseover‘, function () { this.removeAttribute(‘readonly‘); }).on(‘input‘, function () { this.setAttribute(‘ilength‘, this.value.length > item.attributes[‘ilength‘].value ? ++item.attributes[‘ilength‘].value : --item.attributes[‘ilength‘].value); }); var clear = () => { !item.value ? setTimeout(check, 500) : (item.value = ‘‘, setTimeout(clear, 100)); }; var check = () => { item.value.length != item.attributes[‘ilength‘].value ? (layer.tips(‘检测到密码输入异常,已自动拦截‘, item, { tips: [2, ‘#000000‘], time: 2000 }), item.setAttribute(‘ilength‘, 0), clear()) : setTimeout(check, 500); }; check(); }); }
说明:其中提示相关的代码使用的是layui的layer模块,可以换成自己想用的东西,或者不进行任何提示。
<body> <input id="password"/> </body> <script> $(function(){ disabledRememberPassword(‘#password‘); //或者 disabledRememberPassword($(‘#password‘)); }) </script>
<body> <input id="password_0"> <input id="password_1"> ...... </body> <script> $(function(){ disabledRememberPassword(‘#password_0,#password_1‘) //或者 disabledRememberPassword($(‘#password_0,#password_1‘)) }) </script>
<body onload="disabledRememberPassword(‘#password‘)"> <input id="password" /> </body>
其实比较完善的解决方式是
如果这些有帮助到你,记得点个赞哟。有什么疑问的话,可以在评论区留言。
原文:https://www.cnblogs.com/lctr/p/10676271.html