方法:
/** * * 禁止浏览器自动填充密码 * * @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 ? (item.setAttribute('ilength', 0), item.value.length == 0 ? setTimeout(check, 500) : (layer.tips('检测到密码输入异常,已自动拦截', item, { tips: [2, '#000000'], time: 2000 }), clear())) : setTimeout(check, 500); }; check(); }); }
使用:
//单个 disabledRememberPassword('#password'); //多个 disabledRememberPassword('#password,#repassword');