select.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. var legp = function(ele) {
  2. return ({
  3. that: Array.prototype.slice.call(document.querySelectorAll(ele), 0),
  4. stopPropagation: function(e) {
  5. e = e || window.event;
  6. if (e.stopPropagation) { //W3C阻止冒泡方法
  7. e.stopPropagation();
  8. } else {
  9. e.cancelBubble = true; //IE阻止冒泡方法
  10. }
  11. },
  12. hide: function() {
  13. legp(ele).that.forEach(function(item) {
  14. item.style.cssText += "display:none;";
  15. });
  16. },
  17. show: function() {
  18. legp(ele).that.forEach(function(item) {
  19. item.style.cssText += "display:block;";
  20. });
  21. },
  22. legp_searchList: function(name) {
  23. let arr = [];
  24. legp("input[name=" + name + "]").that.forEach(item => {
  25. arr.push(item.value)
  26. });
  27. return arr;
  28. },
  29. legp_search: function(name, tagData) {
  30. var domId = legp(ele).that[0];
  31. var html =
  32. "\n<div>\n<div class=\"selectId\">\n<input type=\"text\" placeholder=\"\u8F93\u5165\u6216\u9009\u62E9\" autocomplete=\"off\">\n<dl style=\"display: none;\">\n</dl>\n</div>\n</div>\n<div>\n<label style=\"line-height:30px\">已选择:</label>\n<div class=\"AD\">\n</div>\n</div>";
  33. domId.innerHTML = html;
  34. // domId.appendChild(dom);
  35. legp(".AD").that[0].parentNode.style.cssText += "display:none;";
  36. //获取当前广告
  37. function myClick() {
  38. legp(".selectId dl dd").that.forEach(function(item) {
  39. item.onclick = function(e) {
  40. var id = this.attributes.value.value;
  41. if (id != '') {
  42. legp(".AD").that[0].innerHTML += "<a href=\"javascript:;\" class=\"label\"><span>" + this.innerHTML +
  43. "</span><input type=\"hidden\" name=\"" + name + "\" id=\"" + id + "\" value=\"" + id +
  44. "\"/><span class=\"close\">x</span></a>";
  45. legp(".AD").that[0].parentNode.style.cssText += "display:block;";
  46. for (var i = 0; i < tagData.length; i++) {
  47. if (tagData[i].id == id) {
  48. tagData.splice(i, 1);
  49. i = tagData.length;
  50. }
  51. }
  52. removeAdvertising();
  53. }
  54. legp(".selectId dl").hide();
  55. legp(".selectId input").that[0].value = '';
  56. e.stopPropagation();
  57. };
  58. });
  59. }
  60. var ddRemove = function(dd) {
  61. var temp = dd.nextElementSibling;
  62. dd.remove();
  63. if (temp != null && temp.nodeName == 'DD') {
  64. ddRemove(temp);
  65. }
  66. };
  67. //筛选
  68. legp(".selectId input").that[0].oninput = function() {
  69. var val = this.value; //获取input值
  70. var dd = legp(".selectId dl dd").that[0];
  71. if (dd == "DD") {
  72. ddRemove(dd);
  73. }
  74. legp(".selectId dl").hide();
  75. if (tagData.length > 0) {
  76. legp(".selectId dl").show();
  77. var sear_1 = new RegExp(val);
  78. var judge_1 = false;
  79. legp(".selectId dl").that[0].innerHTML = "";
  80. tagData.forEach(function(item) {
  81. if (sear_1.test(item.name)) {
  82. judge_1 = true;
  83. legp(".selectId dl").that[0].innerHTML += "<dd value=\"" + item.id + "\">" + item.name + "</dd>";
  84. }
  85. });
  86. if (!judge_1) {
  87. legp(".selectId dl").that[0].innerHTML = "<dd>\u6682\u65E0\u6570\u636E</dd>";
  88. }
  89. myClick();
  90. }
  91. };
  92. //显示没被选择的
  93. legp(".selectId input").that[0].onclick = function(e) {
  94. var dd = legp(".selectId dl dd").that[0];
  95. if (dd == "DD") {
  96. ddRemove(dd);
  97. }
  98. if (tagData.length == 0) {
  99. this.innerHTML = "暂无数据";
  100. } else {
  101. legp(".selectId dl").show();
  102. }
  103. legp(".selectId dl").that[0].innerHTML = "";
  104. tagData.sort(function(a, b) {
  105. return a.id - b.id;
  106. });
  107. tagData.forEach(function(item) {
  108. legp(".selectId dl").that[0].innerHTML += "<dd value=\"" + item.id + "\">" + item.name + "</dd>";
  109. });
  110. myClick();
  111. e.stopPropagation();
  112. };
  113. var AD = /** @class */ (function() {
  114. function AD(name, id) {
  115. this.name = name;
  116. this.id = id;
  117. }
  118. return AD;
  119. }());
  120. //删除当前广告
  121. function removeAdvertising() {
  122. legp(".close").that.forEach(function(item) {
  123. item.onclick = function() {
  124. ddRemove(this.parentNode);
  125. var id = this.parentNode.children[1].id;
  126. var text = this.parentNode.children[0].innerHTML;
  127. tagData.push(new AD(text, id));
  128. if (legp(".close").that.length == 0) {
  129. legp(".AD").that[0].parentNode.style.cssText += "display:none;";
  130. }
  131. };
  132. });
  133. }
  134. // //封装
  135. // function clickoutSide(nameClass, callback) {
  136. // // 全局注册点击事件
  137. // document.onclick = function (e) {
  138. // //若点击元素为目标元素则返回
  139. // if (e.target.className === nameClass) return
  140. // //否则执行回调函数
  141. // callback()
  142. // }
  143. // }
  144. //隐藏
  145. document.onclick = function(e) {
  146. legp(".selectId dl").hide();
  147. legp(".selectId input").that[0].value = "";
  148. };
  149. }
  150. });
  151. };
  152. (function() {
  153. var tagData = [{
  154. "name": "站点1",
  155. "id": 64
  156. }, {
  157. "name": "站点2",
  158. "id": 65
  159. }, {
  160. "name": "站点3",
  161. "id": 5
  162. }, {
  163. "name": "站点4",
  164. "id": 6
  165. }, {
  166. "name": "站点5",
  167. "id": 7
  168. }, {
  169. "name": "站点6",
  170. "id": 8
  171. }, {
  172. "name": "站点7",
  173. "id": 10
  174. }, {
  175. "name": "站点8",
  176. "id": 111
  177. }, {
  178. "name": "站点9",
  179. "id": 112
  180. }, {
  181. "name": "站点10",
  182. "id": 113
  183. }];
  184. legp(".qwe").legp_search("fileId", tagData)
  185. })()