前言:有的時(shí)候我們?cè)O(shè)置input的時(shí)候直接用DW里面的按鈕做成“真焦點(diǎn)”,但是有的時(shí)候可能需要涉及到使用“假焦點(diǎn)”的方式進(jìn)行處理。
關(guān)鍵:
1.外部input.js(其實(shí)就是一個(gè)函數(shù),一個(gè)類)
2.頁(yè)面寫JS調(diào)用外部的input.js代碼,來實(shí)現(xiàn)假焦點(diǎn)光標(biāo)的移動(dòng)和input里面值的添加與刪除
思想:
需要掌握改變類的對(duì)象的屬性,來實(shí)現(xiàn)頁(yè)面光標(biāo)的移動(dòng)和input數(shù)據(jù)的改變。
INPUT.JS:
function input_obj(id,type,start_str,cursor_img,blank_img,img_height,max_world){
this.id = id; //輸入框?qū)?yīng)td的id,可自行修改
this.type = (typeof(type)=="undefined"?'normal':type);
this.start_str = (typeof(start_str)=="undefined"?'':start_str);//文本框中初始的默認(rèn)值
this.cursor_img = (typeof(cursor_img)=="undefined"?'images/focus_input.gif':cursor_img);//光標(biāo)的圖片名稱,默認(rèn)為'focus.gif',可自行修改
this.blank_img = (typeof(blank_img)=="undefined"?'images/global_tm.gif':blank_img);//透明圖片名稱,默認(rèn)為'global_tm.gif',可自行修改
this.img_height = (typeof(img_height)=="undefined"?25:img_height);//圖片的高度,默認(rèn)為25,可自行修改
this.max_world = (typeof(max_world)=="undefined"?6:max_world);//定義最多輸入的字符數(shù),默認(rèn)為4,可自行修改
this.input_timeout = -1;//輸入時(shí)的timeout flag
this.num_list = [
["0"],
[".","1"],
["A","B","C","2","a","b","c"],
["D","E","F","3","d","e","f"],
["G","H","I","4","g","h","i"],
["J","K","L","5","j","k","l"],
["M","N","O","6","m","n","o"],
["P","Q","R","S","7","p","q","r","s"],
["T","U","V","8","t","u","v"],
["W","X","Y","Z","9","w","x","y","z"]
];//輸入法之間切換的內(nèi)容
this.input_str = this.start_str;//輸入的字母
this.list_index = -1;//按下的鍵的位置
this.input_index = 0;//鍵盤中對(duì)應(yīng)字母的位置
this.cursor_pos = this.input_str.length;//標(biāo)記光標(biāo)的位置
this.pwd_mark = "●";//輸入密碼時(shí)顯示出來的星號(hào)
/*-------------------------對(duì)光標(biāo)初始化----------------------------------*/
this.show_cursor=function(){
this.$(this.id).innerHTML = this.get_str();
};
/*-----------------------對(duì)捕獲到的鍵值進(jìn)行處理,并在輸入框中顯示------------------------*/
this.get_input=function(num){
if(this.type == "password"||this.type == "num"){//如果輸入的為密碼或數(shù)字
if(this.input_str.length<this.max_world){//當(dāng)輸入字符數(shù)不小于max_world的時(shí)候?qū)⒉辉夙憫?yīng)
this.input_str=this.input_str.substr(0,this.cursor_pos)+num+this.input_str.substr(this.cursor_pos);
this.cursor_pos++;
this.$(this.id).innerHTML = this.get_str();
}
}else{//輸入的為文字
if(this.list_index == num){//重復(fù)按下同一個(gè)鍵
if(this.input_str.length<(this.max_world+1)){//當(dāng)輸入字符數(shù)大于max_world的時(shí)候?qū)⒉辉夙憫?yīng)
this.input_index++;
if (this.input_index>=this.num_list[num].length) this.input_index=0;
clearTimeout(this.input_timeout);
var self = this;
this.input_timeout = setTimeout(function(){self.list_index=-1}, 800);//超過800毫秒則不認(rèn)為按的是同一個(gè)鍵.
var select_list = this.num_list[this.list_index];
this.input_str = this.input_str.substr(0,(this.cursor_pos-1)) + select_list[this.input_index]+this.input_str.substr(this.cursor_pos);//按同一個(gè)鍵的時(shí)候只改變input_str的最后一個(gè)字母
this.$(this.id).innerHTML = this.get_str();
}
}else{//否則記錄當(dāng)前鍵的位置,并把對(duì)應(yīng)的鍵的字母寫入input_str
if(this.input_str.length<this.max_world){//當(dāng)輸入字符數(shù)不小于max_world的時(shí)候?qū)⒉辉夙憫?yīng)
this.list_index = num;
clearTimeout(this.input_timeout);
var self = this;
this.input_timeout = setTimeout(function(){self.list_index=-1}, 800);//超過800毫秒則不認(rèn)為按的是同一個(gè)鍵.
this.input_index = 0;//還原input_index的值
var select_list = this.num_list[this.list_index];
this.input_str=this.input_str.substr(0,this.cursor_pos)+select_list[this.input_index]+this.input_str.substr(this.cursor_pos);
this.cursor_pos++;
this.$(this.id).innerHTML = this.get_str();
}
}
}
};
/*-------------------------刪除光標(biāo)前面的那個(gè)文字---------------------------*/
this.del_input=function (){
if (this.input_str.length>0&&this.cursor_pos>0){
this.cursor_pos--;
this.input_str = this.input_str.substr(0,this.cursor_pos)+this.input_str.substr(this.cursor_pos+1);
this.$(this.id).innerHTML = this.get_str();
}
};
/*-------------------------移動(dòng)光標(biāo)的位置-----------------------------*/
this.move_input=function(num){
this.cursor_pos+=num;
if(this.cursor_pos<0) this.cursor_pos=0;
else if(this.cursor_pos>this.input_str.length) this.cursor_pos = this.input_str.length;
this.$(this.id).innerHTML = this.get_str();
};
/*----為了讓光標(biāo)在左右移動(dòng)過程中不出現(xiàn)字體晃動(dòng)的現(xiàn)象,
在每個(gè)字符之間都加入了一個(gè)和光標(biāo)大小一樣的透明圖片,下面函數(shù)主要是實(shí)現(xiàn)這個(gè)功能---*/
this.get_str=function(){
var b_img = '<img src='+this.blank_img+' width=2 height='+this.img_height+'>';
var c_img = '<img src='+this.cursor_img+' width=2 height='+this.img_height+'>';
var temp_str = ((this.cursor_pos>0)?b_img:c_img);
if(this.type == "password"){//如果輸入的為密碼
for (var i=0;i<this.input_str.length;i++){
if(i==(this.cursor_pos-1)) temp_str+= this.pwd_mark+c_img;
else temp_str+= this.pwd_mark+b_img;
}
}else if(this.type == "num"){
for (var i=0;i<this.input_str.length;i++){
if(i==(this.cursor_pos-1)) temp_str+= this.input_str.charAt(i)+c_img;
else temp_str+= this.input_str.charAt(i)+b_img;
}
}else{
if(this.cursor_pos>0){
for (var i=0;i<(this.cursor_pos-1);i++){
temp_str+=this.input_str.substr(i,1)+b_img;
}
temp_str+=this.input_str.substr((this.cursor_pos-1),1)+c_img;
}
for (var j=this.cursor_pos;j<this.input_str.length;j++){
temp_str+=this.input_str.substr(j,1)+b_img;
}
}
return temp_str;
};
/*----------------------定義失去焦點(diǎn)時(shí)的操作-------------------------------*/
this.lose_focus = function(){
var b_img = '<img src='+this.blank_img+' width=2 height='+this.img_height+'>';
var temp_str = b_img;
if(this.type == "password"){//如果輸入的為密碼
for (var i=0;i<this.input_str.length;i++){
temp_str+= this.pwd_mark+b_img;
}
}else{
for (var i=0;i<this.input_str.length;i++){
temp_str+= this.input_str.charAt(i)+b_img;
}
}
this.$(this.id).innerHTML = temp_str;
}
/*----------------簡(jiǎn)單的定義一個(gè)獲取id的方法---------------*/
this.$= function(id) {
return document.getElementById(id);
}
};
HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html xmlns="<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>假焦點(diǎn)輸入Case</title>
<script language="javascript" src="input.js"></script>
<script type="text/javascript">
var pos=0;//定義光標(biāo)的起始位置
var inputObjts = new Array();//定義一個(gè)變量,該變量是一個(gè)數(shù)組
inputObjts[0] = new input_obj("input0","num","Admin");//定義一個(gè)類的實(shí)例對(duì)象
inputObjts[1] = new input_obj("input1","password","");
inputObjts[2] = new input_obj("input2","password","");
function showFoucs(__num){
inputObjts[pos].lose_focus();
pos+=__num;
if(pos<0)pos=0;
else if(pos>inputObjts.length-1)pos=inputObjts.length-1;
inputObjts[pos].show_cursor();
}
function init(){
showFoucs(0);
}
document.onsystemevent = grabEvent;
document.onkeypress = grabEvent;
document.onirkeypress=grabEvent;
function grabEvent(){
var key_code = event.which;
switch(key_code){
case 1://up
case 269:
showFoucs(-1);
return 0;
break;
case 2://down
case 270:
showFoucs(1)
return 0;
break;
case 3://left
case 271:
if(inputObjts[pos].input_str.length>0)inputObjts[pos].del_input();
return 0;
break;
case 4://right
case 272:
return 0;
break;
case 340://back
case 339:
return 0;
break;
case 13:
alert(inputObjts[0].input_str+"+"+inputObjts[1].input_str+"+"+inputObjts[2].input_str);
return 0;
break;
case 48:
case 49:
case 50:
case 51:
case 52:
case 53:
case 54:
case 55:
case 56:
case 57:
var num = key_code - 48;
inputObjts[pos].get_input(num);//獲取輸入的數(shù)字
return 0;
break;
}
}
</script>
</head>
<body onload="init();">
<br />
<br />
<br />
<table width="500" border="1" cellspacing="1" cellpadding="1" align="center" style="font-size:24px;">
<tr>
<td width="120" height="40" align="right">用戶名:</td>
<td width="180" id="input0"> </td>
</tr>
<tr>
<td height="40" align="right">密碼:</td>
<td id="input1"> </td>
</tr>
<tr>
<td height="40" align="right">驗(yàn)證碼:</td>
<td id="input2"> </td>
</tr>
</table>
</body>
</html>
聯(lián)系客服