tab.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. 彭佳文 李泉 input补全控件
  3. */
  4. Namespace.register("U.Control.TAB");
  5. /*window.onload = function () {
  6. U.Control.TAB.start();
  7. }*/
  8. U.Control.TAB = function (elm, options) {
  9. /**
  10. *
  11. * get input position
  12. */
  13. function getPosition(obj) { //定位
  14. var t = obj.offsetTop + obj.offsetHeight;
  15. var l = obj.offsetLeft;
  16. while (obj = obj.offsetParent) {
  17. t += obj.offsetTop;
  18. l += obj.offsetLeft;
  19. }
  20. return { t: t, l: l }
  21. }
  22. U.Control.TAB.scroe = 0;
  23. /**
  24. * initial set
  25. *
  26. * @param {HTMLElEMENT} elm
  27. * @param {JSON} lib
  28. */
  29. var elm = this.elm = elm[0] || elm, lib = this.lib = lib, ordinate = getPosition(elm);
  30. var t = this.t = ordinate.t;
  31. var l = this.l = ordinate.l;
  32. var box = this.box = document.createElement("div");
  33. this.box.style.cssText = "position:absolute;left:" + l + ";top:" + t + ";width:384px;border:1px solid #E4E4E4;color:#B3B3B3;display:none; top: 299px;left: 462px;z-index:999"
  34. box.id = 'box';
  35. document.body.appendChild(this.box);
  36. /**
  37. * initial input event
  38. */
  39. elm.addEventListener("keyup", function () {//给text附函数
  40. $("#box")[0].style.display = "block";
  41. var content = this.value;
  42. if (content.charAt(0) == "a" || content.charAt(0) == "b") {
  43. document.addEventListener("click", function () {
  44. $("#box")[0].style.display = "none;";
  45. $("#bi")[0].style.display = "none";
  46. U.Control.TAB.scroe = 0;
  47. });
  48. box.innerHTML = "";
  49. for (var i = 0; i < auto[content].length; i++) {
  50. var p = document.createElement("div");
  51. p.innerText = auto[content][i];
  52. p.id = auto[content][i];
  53. p.style.cssText = "width:336px;height:45px;line-height:48px;border-bottom:1px solid #F9F9F9;padding: 0px 24px;line-height: 45px;top: 427px;";
  54. p.onclick = function () {
  55. elm.value = this.id;
  56. box.style.display = "none";
  57. $("#bi")[0].style.diplay = "none";
  58. }
  59. p.onmouseover = function () {
  60. this.style.backgroundColor = '#F1F1F1';
  61. this.childNodes[1].style.display = "block";
  62. }
  63. p.onmouseout = function () {
  64. this.style.backgroundColor = 'white';
  65. this.childNodes[1].style.display = "none";
  66. }
  67. var a = document.createElement("a");
  68. a.innerHTML = "删除";
  69. a.href = "#";
  70. a.style.cssText = "float:right;font-size: 13px;color: rgb(179, 179, 179);display:none";
  71. a.onclick = function (e) {
  72. stopPropagation(e);
  73. this.parentNode.remove();
  74. }
  75. p.appendChild(a);
  76. box.appendChild(p);
  77. }
  78. if ($("#bi")[0]) {
  79. $("#bi")[0].style.display = "block";
  80. return;
  81. }
  82. else {
  83. var qk_box = document.createElement("div");
  84. qk_box.style.cssText = "position:absolute;left:462px;top:530px;width:336px;height:45px;line-height:48px;border:1px solid rgb(228, 228, 228);padding: 0px 24px;line-height: 45px;display:block;z-index:999;";
  85. qk_box.id = "bi";
  86. document.body.appendChild(qk_box);
  87. var gd = document.createElement("a");
  88. gd.innerHTML = "更多历史";
  89. gd.href = "#";
  90. gd.style.cssText = "float:right;font-size: 12px;color:#9C9C9C;";
  91. qk_box.appendChild(gd);
  92. var qk = document.createElement("a");
  93. qk.innerHTML = "关闭历史";
  94. qk.href = "#";
  95. qk.style.cssText = "float:right;font-size: 12px;color:#9C9C9C;margin-right:10px;";
  96. qk.onclick = function () {
  97. $("#box")[0].innerHTML = "";
  98. $("#bi").style.display = "none";
  99. U.Control.TAB.scroe = 0;
  100. }
  101. qk_box.appendChild(qk);
  102. }
  103. }
  104. else {
  105. $("#box")[0].innerHTML = "";
  106. //$("#bi").style.display = "none";
  107. U.Control.TAB.scroe = 0;
  108. }
  109. })
  110. /*
  111. 下拉框键盘 上下键更换 ENTER键消失下拉框
  112. */
  113. elm.onkeydown = function (e) {
  114. var aa = e || event;
  115. var last = box.childNodes.length;
  116. if (aa.keyCode == 38) { //上
  117. if (0 == U.Control.TAB.scroe) {
  118. U.Control.TAB.scroe = box.childNodes.length;
  119. }
  120. if (U.Control.TAB.scroe == 1) {
  121. box.childNodes[0].style.backgroundColor = '';
  122. box.childNodes[0].childNodes[1].style.display = "none";
  123. box.childNodes[last - 1].style.backgroundColor = '#F1F1F1';
  124. box.childNodes[last - 1].childNodes[1].style.display = 'block';
  125. elm.value = box.childNodes[last - 1].id;
  126. }
  127. if (U.Control.TAB.scroe - 2 <= 0) {
  128. box.childNodes[0].style.backgroundColor = '#F1F1F1';
  129. box.childNodes[0].childNodes[1].style.display = "block";
  130. }
  131. else {
  132. box.childNodes[U.Control.TAB.scroe - 2].style.backgroundColor = '#F1F1F1';
  133. box.childNodes[U.Control.TAB.scroe - 2].childNodes[1].style.display = "block";
  134. }
  135. //if(U.Control.TAB.scroe==0 ) box.childNodes[box.childNodes.length-1].style.backgroundColor='';
  136. box.childNodes[U.Control.TAB.scroe - 1].style.backgroundColor = '';
  137. box.childNodes[U.Control.TAB.scroe - 1].childNodes[1].style.display = "none";
  138. U.Control.TAB.scroe -= 1;
  139. elm.value = box.childNodes[U.Control.TAB.scroe - 1].id;
  140. }
  141. if (aa.keyCode == 13) { //enter
  142. box.innerHTML = "";
  143. $("#bi")[0].style.display = "none";
  144. U.Control.TAB.scroe = 0;
  145. }
  146. if (aa.keyCode == 40) { //下
  147. if (box.childNodes.length == U.Control.TAB.scroe) {
  148. U.Control.TAB.scroe = 0;
  149. }
  150. box.childNodes[U.Control.TAB.scroe].style.backgroundColor = '#F1F1F1';
  151. box.childNodes[U.Control.TAB.scroe].childNodes[1].style.display = 'block';
  152. if (U.Control.TAB.scroe == 0) {
  153. box.childNodes[box.childNodes.length - 1].style.backgroundColor = '';
  154. box.childNodes[box.childNodes.length - 1].childNodes[1].style.display = 'none';
  155. }
  156. else if (U.Control.TAB.scroe >= 1) {
  157. box.childNodes[U.Control.TAB.scroe - 1].style.backgroundColor = '';
  158. box.childNodes[U.Control.TAB.scroe - 1].childNodes[1].style.display = 'none';
  159. }
  160. elm.value = box.childNodes[U.Control.TAB.scroe].id;
  161. U.Control.TAB.scroe += 1;
  162. }
  163. }
  164. }
  165. function stopPropagation(e) {//阻止冒泡
  166. e = e || window.event;
  167. if (e.stopPropagation) {
  168. e.stopPropagation();
  169. } else {
  170. e.cancelBubble = true;
  171. }
  172. }
  173. U.Control.TAB.start = function () { //初始化
  174. auto = {
  175. "a": ["abc", "ass", "abd", "aaaaa", "acc", ],
  176. "b": ["bag", "bit", "bug", "bsd", "banner"]
  177. }
  178. var g = $$("div", {}, $('body')[0]);
  179. var best = $$("div", { "id": "best" }, g);
  180. var a = $$("input", { "id": "a" }, best);
  181. var btn = $$("div", { "id": "btn", "innerHTML": "搜索" }, best);
  182. U.Control.TAB.dei(a,auto)
  183. return g;
  184. }
  185. U.Control.TAB.dei = function (a,json) {
  186. U.Control.TAB(a,json);
  187. //U.Control.TAB(document.getElementById("b"), json);
  188. }