`

js 軟鍵盤

阅读更多

今天在CSDN中發現有一篇文章挺有意思 ,博文地址是:http://blog.csdn.net/java2000_net/article/details/6237169

特意轉載 ,文章版權歸原作者所有.下載源碼有點問題,我已改正,如下所示:

           function _softkeyboard_(target,container){
                      var _ = _softkeyboard_;
                      _.target = target;
                      _.doc = container.ownerDocument;
                      (_.doc.parentWindow||_.doc.defaultView)._softkeyboard_ = _softkeyboard_;
                      _.skb = container;
                      _.skb.innerHTML = _.getkeyshtml();
                      var toggle = (_.skb.style.display == "block");
                      _.skb.style.display = toggle ? "none" : "block";
                      _.preventKey(target);
                      return toggle;
           }
 
           function closeKeyBoard()
           {
                      document.getElementById('skb').style.display = "none";
           }
 
           _softkeyboard_.shifted = true;
           _softkeyboard_.getkeyshtml = function(shifted){
                      var htm = [], keys = _softkeyboard_.shifted ? 
                                 ["`1234567890-=","backspace","<br/>"," ","qwertyuiop[]//","<br/>"," "," ","asdfghjkl;","'","<br/>","shift","zxcvbnm,./","shift"] : 
                                 ["~!@#$%^&*()_+","backspace","<br/>"," ","QWERTYUIOP{}|","<br/>"," "," ","ASDFGHJKL:",'"',"<br/>","shift","ZXCVBNM<>?","shift"];
                      var esp = {
                                            "'":'<input type="button" value="/" onclick="_softkeyboard_.onkeydown(this);" />',
                                            '"':"<input type='button' value='/' onclick='_softkeyboard_.onkeydown(this);' />",
                                            "<br/>":"<br/>",
                                            " ":" ",
                                            "backspace":'<input type="button" value="<-" onclick="_softkeyboard_.backspace();" />',
                                            "shift":'<input type="button" style="width:50px;" value="shift" onclick="_softkeyboard_.shifted=!_softkeyboard_.shifted;_softkeyboard_.skb.innerHTML = _softkeyboard_.getkeyshtml();" />'
                      };
                      for(var j = 0; j < keys.length; j++){
                                 if(esp[keys[j]]){
                                            htm.push(esp[keys[j]]);
                                 }
                                 else{
                                            var x = new Date().getMilliseconds()%keys[j].length;
                                            keys[j] =  keys[j].substr(x,keys[j].length) + keys[j].substr(0,x);
                                            for(var i = 0; i < keys[j].length; i++){    
                                                       htm.push("<input type='button' class='key' value='"+ keys[j].charAt(i)+"' onclick='_softkeyboard_.onkeydown(this);' />");
                                            }
                                 }
                      }
                      htm.push("<div style= 'margin-top:5px;' mce_style='margin-top:5px;'><input type='button' onclick='closeKeyBoard()' value='确定' style='margin-right:20px;' mce_style='margin-right:20px;' /><input type='button' onclick='closeKeyBoard()' value='关闭' /><div/>");
                      return htm.join("");
           }
           _softkeyboard_.onkeydown = function(ele){
                      _softkeyboard_.target.value += ele.value;
                      _softkeyboard_.target.focus();
           }
           _softkeyboard_.backspace = function(){
                      with(_softkeyboard_.target){
                                 value = value.substr(0,value.length-1);
                      }
           }
           _softkeyboard_.addCSSRule = function(key,value){}
           _softkeyboard_.hide = function(){
                      if(_softkeyboard_.skb){
                                 _softkeyboard_.skb.style.display = "none"
                      }
           }
           _softkeyboard_.preventKey = function(target){
                      var targetkeydown = function(e){
                                 e = window.event?window.event:e;
                                 switch(e.keyCode){
                                            case 27:// esc
                                            case 9:// tab
                                                       _softkeyboard_.hide();
                                                       if(window.event){e.returnValue = false;}else{e.preventDefault();}break;
                                 }
                      };
                      // target.addEventListener ?
						// target.addEventListener("keydown",targetkeydown,false)
						// : target.attachEvent("onkeydown",targetkeydown);
           }
 
           window.top._softkeyboard_=_softkeyboard_;
           _softkeyboard_.preventKey(window.top.document.body);
 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics