From 0ef854b98fe56b4d2884a70639465244b0eba5d3 Mon Sep 17 00:00:00 2001 From: vczero Date: Thu, 23 Apr 2015 17:01:42 +0800 Subject: [PATCH] add test --- test/index.html | 50 ++++++++++++++++++++++++++++ test/keyboard.js | 86 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 test/index.html create mode 100644 test/keyboard.js diff --git a/test/index.html b/test/index.html new file mode 100644 index 0000000..677b2bb --- /dev/null +++ b/test/index.html @@ -0,0 +1,50 @@ + + + + + 模拟数字键盘 + + + +
+ +
+
+ +
+ + + + diff --git a/test/keyboard.js b/test/keyboard.js new file mode 100644 index 0000000..119b9f1 --- /dev/null +++ b/test/keyboard.js @@ -0,0 +1,86 @@ + +;(function(exports){ + var KeyBoard = function(input, options){ + var body = document.getElementsByTagName('body')[0]; + var DIV_ID = options && options.divId || '__w_l_h_v_c_z_e_r_o_divid'; + + if(document.getElementById(DIV_ID)){ + body.removeChild(document.getElementById(DIV_ID)); + } + + this.input = input; + this.el = document.createElement('div'); + + var self = this; + var zIndex = options && options.zIndex || 1000; + var width = options && options.width || '100%'; + var height = options && options.height || '193px'; + var fontSize = options && options.fontSize || '15px'; + var backgroundColor = options && options.backgroundColor || '#fff'; + var TABLE_ID = options && options.table_id || 'table_0909099'; + var mobile = typeof orientation !== 'undefined'; + + this.el.id = DIV_ID; + this.el.style.position = 'absolute'; + this.el.style.left = 0; + this.el.style.right = 0; + this.el.style.bottom = 0; + this.el.style.zIndex = zIndex; + this.el.style.width = width; + this.el.style.height = height; + this.el.style.backgroundColor = backgroundColor; + + //样式 + var cssStr = ''; + + //Button + var btnStr = '
完成
'; + + //table + var tableStr = ''; + tableStr += ''; + tableStr += ''; + tableStr += ''; + tableStr += ''; + tableStr += ''; + tableStr += '
123
456
789
.0删除
'; + this.el.innerHTML = cssStr + btnStr + tableStr; + + function addEvent(e){ + var ev = e || window.event; + var clickEl = ev.element || ev.target; + var value = clickEl.textContent || clickEl.innerText; + if(clickEl.tagName.toLocaleLowerCase() === 'td' && value !== "删除"){ + if(self.input){ + self.input.value += value; + } + }else if(clickEl.tagName.toLocaleLowerCase() === 'div' && value === "完成"){ + body.removeChild(self.el); + }else if(clickEl.tagName.toLocaleLowerCase() === 'td' && value === "删除"){ + var num = self.input.value; + if(num){ + var newNum = num.substr(0, num.length - 1); + self.input.value = newNum; + } + } + } + + if(mobile){ + this.el.ontouchstart = addEvent; + }else{ + this.el.onclick = addEvent; + } + body.appendChild(this.el); + } + + exports.KeyBoard = KeyBoard; + +})(window);