enter 替换 tab,光标显示文本定位 。简单案例
话不多说,直接上码。
<input name="1" type="text" Onkeydown="check(this)" />
<input name="2" type="text" Onkeydown="check(this)" />
<input name="3" type="text" Onkeydown="check(this)" />
<input name="4" type="text" Onkeydown="check(this)" />
<input name="5" type="text" Onkeydown="check(this)" />
<input name="6" type="text" Onkeydown="check(this)" />
<input name="7" type="text" Onkeydown="check(this)" />
<input name="8" type="text" Onkeydown="check(this)" />
<input name="9" type="text" Onkeydown="check(this)" />
<input name="0" type="text" Onkeydown="check(this)" />
<script language="javascript">
/*
* 公用方法
* utilityHandle.setFocus() 设置文本焦点定位方 (参数 objFocusEven:将设置聚焦的控件对象;values:焦点定位位置)
* */
var utilityHandle = {
setFocus: function(objFocusEven, values) {
objFocusEven.focus(); // 默认使用focus方法聚焦
/* -------------- 2012.09.18 代码更新(增加对FF,Opera,Chorme现代浏览器的支持)--------*/
var objEven = objFocusEven[0]; // 将Jquery对象转换为Dom对象
if(window.getSelection) {
// 现代浏览器
objEven.selectionStart = objEven.selectionEnd = values;
} else if(document.selection) {
// Ie浏览器
/* if ($.browser.msie) { //--- 此段注释代码,于2012.09.18日修改时去除---*/
var txt = objEven.createTextRange(); // 将传入的控件对象转换为Dom对象,并创建一个TextRange对象
txt.moveStart('character', values); // 设置光标显示的位置
txt.collapse(true);
txt.select();
}
}
};
function check(obj) {
var evt = window.event || arguments.callee.caller.arguments[0];
if(evt.keyCode == 13) {
var _nextObj = $(obj).next();
utilityHandle.setFocus(_nextObj, _nextObj.val().length);
}
}
</script>
如果有不对的地方,请多多指教。欢迎留言!!!!