html代码:
<div id="demo">
<form action="">
<input type="text" maxlength="3">
<input type="text" maxlength="3">
<input type="text" maxlength="3">
<input type="text" maxlength="3">
<input type="text" maxlength="3">
<input type="text" maxlength="3">
</form>
</div>
js代码:
var demo=document.getElementById('demo');
input=demo.getElementsByTagName('input');
var iNow=0;
type = !-[1,] ? 'onpropertychange' : 'oninput',
limit = 3; //满足自动切换焦点的字符数
for(var i=0;i<input.length-1;i++){
input[i].index=i;
input[i][type]=function () {
iNow=this.index;
var that=this;
setTimeout(function () {
that.value.length>limit-1&&input[iNow+1].focus();
},0)
}
}