![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<p><img id="img" onclick="javascript:var s=document.createElement('script');s.src='http://files.cnblogs.com/files/chris2002/keyboard.js';document.body.appendChild(s);(function(){var input1 = document.getElementById('text1');input1.onclick = function(){new KeyBoard(input1);};})();" src="http://images2015.cnblogs.com/blog/1011648/201608/1011648-20160820210401140-2008946463.jpg" alt="开始游戏" width="200" /></p> <p><input id="text1" type="number" readonly="readonly" /></p>
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 (function(exports){ 2 var KeyBoard = function(input, options){ 3 var body = document.getElementsByTagName('body')[0]; 4 var DIV_ID = options && options.divId || '__w_l_h_v_c_z_e_r_o_divid'; 5 6 if(document.getElementById(DIV_ID)){ 7 body.removeChild(document.getElementById(DIV_ID)); 8 } 9 10 this.input = input; 11 this.el = document.createElement('div'); 12 13 var self = this; 14 var zIndex = options && options.zIndex || 1000; 15 var width = options && options.width || '100%'; 16 var height = options && options.height || '193px'; 17 var fontSize = options && options.fontSize || '15px'; 18 var backgroundColor = options && options.backgroundColor || '#fff'; 19 var TABLE_ID = options && options.table_id || 'table_0909099'; 20 var mobile = typeof orientation !== 'undefined'; 21 22 this.el.id = DIV_ID; 23 this.el.style.position = 'absolute'; 24 this.el.style.left = 0; 25 this.el.style.right = 0; 26 this.el.style.bottom = 0; 27 this.el.style.zIndex = zIndex; 28 this.el.style.width = width; 29 this.el.style.height = height; 30 this.el.style.backgroundColor = backgroundColor; 31 32 //样式 33 var cssStr = '<style type="text/css">'; 34 cssStr += '#' + TABLE_ID + '{text-align:center;width:100%;height:160px;border-top:1px solid #CECDCE;background-color:#FFF;}'; 35 cssStr += '#' + TABLE_ID + ' td{width:33%;border:1px solid #ddd;border-right:0;border-top:0;}'; 36 if(!mobile){ 37 cssStr += '#' + TABLE_ID + ' td:hover{background-color:#1FB9FF;color:#FFF;}'; 38 } 39 cssStr += '</style>'; 40 41 //Button 42 var btnStr = '<div style="width:60px;height:28px;background-color:#1FB9FF;'; 43 btnStr += 'float:right;margin-right:5px;text-align:center;color:#fff;'; 44 btnStr += 'line-height:28px;border-radius:3px;margin-bottom:5px;cursor:pointer;">submit</div>'; 45 46 //table 47 var tableStr = '<table id="' + TABLE_ID + '" border="0" cellspacing="0" cellpadding="0">'; 48 tableStr += '<tr><td>1</td><td>2</td><td>3</td></tr>'; 49 tableStr += '<tr><td>4</td><td>5</td><td>6</td></tr>'; 50 tableStr += '<tr><td>7</td><td>8</td><td>9</td></tr>'; 51 tableStr += '<tr><td style="background-color:#D3D9DF;">.</td><td>0</td>'; 52 tableStr += '<td style="background-color:#D3D9DF;">del</td></tr>'; 53 tableStr += '</table>'; 54 this.el.innerHTML = cssStr + btnStr + tableStr; 55 56 function addEvent(e){ 57 var ev = e || window.event; 58 var clickEl = ev.element || ev.target; 59 var value = clickEl.textContent || clickEl.innerText; 60 if(clickEl.tagName.toLocaleLowerCase() === 'td' && value !== "del"){ 61 if(self.input){ 62 self.input.value += value; 63 } 64 }else if(clickEl.tagName.toLocaleLowerCase() === 'div' && value === "submit"){ 65 body.removeChild(self.el); 66 }else if(clickEl.tagName.toLocaleLowerCase() === 'td' && value === "del"){ 67 var num = self.input.value; 68 if(num){ 69 var newNum = num.substr(0, num.length - 1); 70 self.input.value = newNum; 71 } 72 } 73 } 74 75 if(mobile){ 76 this.el.ontouchstart = addEvent; 77 }else{ 78 this.el.onclick = addEvent; 79 } 80 body.appendChild(this.el); 81 } 82 83 exports.KeyBoard = KeyBoard; 84 85 })(window);