边距实际是没有的啦~,途中只是为了看着更清晰一些。
另外,我们需要把顶层的真是输入框opcity设为0,这样其实呈现在用户面前的就是这一组伪造的输入框啦。
但是用户其实再输入的时候还是对真是输入框进行操作,在之后我们在讲用户的输入依次填写到伪造输入框里边就可以喽~
很简单吧~
二. 码代码
首先初始化各个DOM,以及绑定输入事件。
function init(fun){var that = this;this.callback = fun;that.realInput = container.children[0];that.bogusInput = container.children[1];that.bogusInputArr = that.bogusInput.children;that.maxLength = that.bogusInputArr[0].getAttribute("maxlength");that.realInput.oninput = function(){that.setValue();}that.realInput.onpropertychange = function(){that.setValue();}}然后在用户输入时分别将用户输入输入到伪造输入框中
function setValue(){this.realInput.value = this.realInput.value.replace(/D/g,"");console.log(this.realInput.value.replace(/D/g,""))var real_str = this.realInput.value;for(var i = 0 ; i < this.maxLength ; i++){this.bogusInputArr[i].value = real_str[i]?real_str[i]:"";}if(real_str.length >= this.maxLength){this.realInput.value = real_str.substring(0,6);this.callback();}}最后我们输入密码当然是要获取的啦~,来一个获取最终值的方法~
function getBoxInputValue(){var realValue = "";for(var i in this.bogusInputArr){if(!this.bogusInputArr[i].value){break;}realValue += this.bogusInputArr[i].value;}return realValue;}以上所述是小编给大家介绍的JS实现六位字符密码输入器功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!