js多方框输入密码_js 如何实现密码输入时,明文显示一秒后变成密文呢?

function MaskedPassword(passfield, symbol)

{

if(typeof document.getElementById == 'undefined'

|| typeof document.styleSheets == 'undefined') { return false; }

if(passfield == null) { return false; }

this.symbol = symbol;

this.isIE = typeof document.uniqueID != 'undefined';

passfield.value = '';

passfield.defaultValue = '';

passfield._contextwrapper = this.createContextWrapper(passfield);

this.fullmask = false;

var wrapper = passfield._contextwrapper;

var hiddenfield = '';

var textfield = this.convertPasswordFieldHTML(passfield);

wrapper.innerHTML = hiddenfield + textfield;

passfield = wrapper.lastChild;

passfield.className += ' masked';

passfield.setAttribute('autocomplete', 'off');

passfield._realfield = wrapper.firstChild;

passfield._contextwrapper = wrapper;

var self = this;

this.addListener(passfield, 'change', function(e)

{

self.fullmask = false;

self.doPasswordMasking(self.getTarget(e));

});

this.addListener(passfield, 'input', function(e)

{

self.fullmask = false;

self.doPasswordMasking(self.getTarget(e));

});

this.addListener(passfield, 'propertychange', function(e)

{

self.doPasswordMasking(self.getTarget(e));

});

this.addListener(passfield, 'keyup', function(e)

{

if(!/^(9|1[678]|224|3[789]|40)$/.test(e.keyCode.toString()))

{

self.fullmask = false;

self.doPasswordMasking(self.getTarget(e));

}

});

this.addListener(passfield, 'blur', function(e)

{

self.fullmask = true;

self.doPasswordMasking(self.getTarget(e));

});

this.forceFormReset(passfield);

return true;

}

MaskedPassword.prototype =

{

doPasswordMasking : function(textbox)

{

var plainpassword = '';

if(textbox._realfield.value != '')

{

for(var i=0; i

{

if(textbox.value.charAt(i) == this.symbol)

{

plainpassword += textbox._realfield.value.charAt(i);

}

else

{

plainpassword += textbox.value.charAt(i);

}

}

}

else

{

plainpassword = textbox.value;

}

var maskedstring = this.encodeMaskedPassword(plainpassword, this.fullmask, textbox);

if(textbox._realfield.value != plainpassword || textbox.value != maskedstring)

{

textbox._realfield.value = plainpassword;

textbox.value = maskedstring;

}

},

encodeMaskedPassword : function(passwordstring, fullmask, textbox)

{

var characterlimit = fullmask === true ? 0 : 1;

for(var maskedstring = '', i=0; i

{

if(i < passwordstring.length - characterlimit)

{

maskedstring += this.symbol;

}

//otherwise just copy across the real character

else

{

maskedstring += passwordstring.charAt(i);

}

}

return maskedstring;

},

createContextWrapper : function(passfield)

{

var wrapper = document.createElement('span');

wrapper.style.position = 'relative';

passfield.parentNode.insertBefore(wrapper, passfield);

wrapper.appendChild(passfield);

return wrapper;

},

forceFormReset : function(textbox)

{

while(textbox)

{

if(/form/i.test(textbox.nodeName)) { break; }

textbox = textbox.parentNode;

}

if(!/form/i.test(textbox.nodeName)) { return null; }

this.addSpecialLoadListener(function() { textbox.reset(); });

return textbox;

},

convertPasswordFieldHTML : function(passfield, addedattrs)

{

var textfield = '

for(var fieldattributes = passfield.attributes,

j=0; j

{

if(fieldattributes[j].specified && !/^(_|type|name)/.test(fieldattributes[j].name))

{

textfield += ' ' + fieldattributes[j].name + '="' + fieldattributes[j].value + '"';

}

}

textfield += ' type="text" autocomplete="off">';

return textfield;

},

limitCaretPosition : function(textbox)

{

//create a null timer reference and start function

var timer = null, start = function()

{

if(timer == null)

{

//IE uses this range stuff

if(this.isIE)

{

timer = window.setInterval(function()

{

var range = textbox.createTextRange(),

valuelength = textbox.value.length,

character = 'character';

range.moveEnd(character, valuelength);

range.moveStart(character, valuelength);

range.select();

}, 100);

}

else

{

timer = window.setInterval(function()

{

var valuelength = textbox.value.length;

if(!(textbox.selectionEnd == valuelength && textbox.selectionStart <= valuelength))

{

textbox.selectionStart = valuelength;

textbox.selectionEnd = valuelength;

}

}, 100);

}

}

},

stop = function()

{

window.clearInterval(timer);

timer = null;

};

this.addListener(textbox, 'focus', function() { start(); });

this.addListener(textbox, 'blur', function() { stop(); });

},

addListener : function(eventnode, eventname, eventhandler)

{

if(typeof document.addEventListener != 'undefined')

{

return eventnode.addEventListener(eventname, eventhandler, false);

}

else if(typeof document.attachEvent != 'undefined')

{

return eventnode.attachEvent('on' + eventname, eventhandler);

}

},

addSpecialLoadListener : function(eventhandler)

{

if(this.isIE)

{

return window.attachEvent('onload', eventhandler);

}

else

{

return document.addEventListener('DOMContentLoaded', eventhandler, false);

}

},

getTarget : function(e)

{

if(!e) { return null; }

return e.target ? e.target : e.srcElement;

}

}

setTimeout("$('pass').")

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值