最近做了一个input输入后根据input输入框的长度自动跳转,并且变色的功能,包括适用单独的某个input 或者整个table
单独input 的处理
<script type="text/javascript">
function changebg(obj){
var currObj = document.getElementById( obj.id);
currObj.style.backgroundColor = 'B0E0E6';
if (currObj.value.length == currObj.maxLength) {
currObj.style.backgroundColor = 'white';
var nextObj = $(obj).parent().next().find("input");
if (nextObj[0] != null) {
nextObj[0].focus();
nextObj[0].style.backgroundColor ='B0E0E6';
//nextObj.addClass('selected')
}
}
}
function removeStyle(obj){
var currObj = document.getElementById(obj.id);
currObj.style.backgroundColor = '';
}
</script>
适合整个table的情况:如下
window.onload = function(){
var table_1 = document.getElementById("table1");
var rows_1 = table_1.rows;
for (var i = 0, leni = rows_1.length; i < leni; i++) {
var cells_1 = rows_1[i].cells;
for (var j = 0, lenj = cells_1.length; j < lenj; j++) {
var input_1 = cells_1[j].children[0];
input_1.id = 'table_1_'+ i + '_' + j;
input_1.maxlength = 1;
var td_c = new changebg(i, leni - 1, j, lenj - 1,'table_1_');
input_1.onkeyup = td_c.oper;
input_1.onblur = td_c.oper2;
}
}
function changebg(i, leni, j, lenj,tab_id){
this.oper = function(){
var currObj = document.getElementById( tab_id + i + '_' + j);
currObj.style.backgroundColor = 'B0E0E6';
if (currObj.value.length == currObj.maxlength) {
currObj.style.backgroundColor = 'white';
var nextObj = j < lenj ? document.getElementById(tab_id + i + '_' + (j + 1)) : i < leni ? nextObj = document.getElementById((tab_id + i + 1) + '_0') : null;
if (nextObj != null) {
nextObj.focus();
nextObj.style.backgroundColor = 'B0E0E6';
}
}
}
this.oper2 = function(){
var currObj = document.getElementById(tab_id + i + '_' + j);
currObj.style.backgroundColor = '';
}
}