平時感覺Windows的IP輸入框很好用,如果再網頁中也能實現一定是很爽的事情。核心代碼如下:
<script language="javascript"> function outStr(){ if(document.all.ip1.value>0 && document.all.ip2.value>0 && document.all.ip3.value>0 && document.all.ip4.value>0){ alert("你輸入的IP地址為:" + document.all.ip1.value + "." + document.all.ip2.value + "." + document.all.ip3.value + "." + document.all.ip4.value); }//end if }//end function moveRight(obj){ id = parseInt(obj.name.substr(2,1)) if(id<4){ eval("document.all.ip" + (id +1) + ".focus()"); }else{ outStr(); }//end if } function moveLeft(obj){ id = parseInt(obj.name.substr(2,1)) if(id>1){ eval("document.all.ip" + (id -1) + ".focus()"); }//end if }//end if function Keypress(obj){ var objInput = obj; cod = window.event.keyCode;
if(cod==46 && (obj.value).length>0){//如果按了“.”并且有了一個字符,向右移動一次 window.event.keyCode=0; moveRight(obj); }else if((obj.value).length==2 && (cod >= 48) && (cod <= 57)){//如果數字鍵并且夠了兩位 if(cod<58){// obj.value = obj.value * 10 + cod - 48; }//end if window.event.keyCode = 0; moveRight(obj); }else{ //判斷輸入的是不是數字 if ( !(((cod >= 48) && (cod <= 57)) (cod == 13) (cod == 37) (cod == 39))){ window.event.keyCode = 0; }//end if }//end if }//end function keydown function getCaretPos(obj){ var currentRange=document.selection.createRange(); var workRange=currentRange.duplicate(); obj.select(); var allRange=document.selection.createRange(); var len=0; while(workRange.compareEndPoints("StartToStart",allRange)>0){ workRange.moveStart("character",-1); len++; } currentRange.select(); return len; }//end function KeyDown(obj){ cod = window.event.keyCode; i = getCaretPos(obj); //光標位置 n = obj.value.length; //字符長度
if(cod==37 && i==0){//光標左移 moveLeft(obj); }else if(cod==39 && i>=n){ moveRight(obj); }else if(cod==8 && (obj.value=="" i==0)){ moveLeft(obj); }//end if }//end function checkDate(obj,max_num){ if(obj.value>max_num obj.value<0){ window.event.keyCode = 0; alert(obj.value + "是無效的項目,只能輸入0到" + max_num + "之間的數。"); obj.value = max_num; obj.focus(); obj.select(); }//end if }//end </script>
測試網址:http://yuenshui.3c21.com/test/ipv1.0.htm 源代碼全部在網頁中,沒有任何加密,希望使用時不要去掉出處。
|