我想出了一个解决这个问题的方法,那就是使用 JQuery 进行密码屏蔽。
提示:密码建议和表单填写仅在输入框类型为密码时才会发生。
所以我的解决方案如下,
01)。使输入字段 type="text"
<input type="text" />
02)。用“•”屏蔽输入字符
var actualPassword = [],
temperoryPassword,
currentCursorPosition = 0,
passwordChar = '•';
$("#password-text").bind("input", function () {
temperoryPassword = $(this).val();
var passwordLength = temperoryPassword.length;
for (var i = 0; i < passwordLength; i++) {
if (temperoryPassword[i] != passwordChar) {
actualPassword[i] = temperoryPassword[i];
}
}
// Get current cursor position.
currentCursorPosition = this.selectionStart;
$(this).val(temperoryPassword.replace(/./g, passwordChar));
});
$("#password-text").bind("keyup", function () {
var passwordLength = $(this).val().length;
if (passwordLength < actualPassword.length) {
var difference = actualPassword.length - passwordLength,
key = event.keyCode || event.charCode;
// Check if last keypress was backspace or delete
if (key == 8 || key == 46) {
actualPassword.splice(currentCursorPosition, difference);
}
// User highlighted and overwrite part of the password
else {
actualPassword.splice(currentCursorPosition - 1, difference + 1);
actualPassword.splice(currentCursorPosition - 1, 0, temperoryPassword[currentCursorPosition - 1]);
}
}
});
$("#btnSubmit").click(function(){
$("#passwordTxt").text(bindactualPassword(actualPassword));
});
// disable password cut, copy and paste function in password field
$('#password-text').bind("cut copy paste",function(e) {
e.preventDefault();
});
function bindactualPassword(){
return actualPassword.join("");
}
我写了一篇关于这个案例的中等文章。你也可以在这里阅读,
希望这对其他人有用:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句