keyWord.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. $(function(){
  2. $(".enterOne").focus(function(){
  3. $(".btnChoose").show();
  4. });
  5. $('.knowBtn').on('click',function(){
  6. $(".problemPop").hide()
  7. });
  8. $(".problem").on('click',function(){
  9. $(".problemPop").css("display",'flex');
  10. });
  11. function hasWords () {
  12. var showKeyWordLength = $(".showKeyWord ul").find('li').length;
  13. if(showKeyWordLength === 0){
  14. $(".addkeyWord").show();
  15. $(".showKeyWord").hide();
  16. } else {
  17. $(".addkeyWord").hide();
  18. $(".showKeyWord").show();
  19. }
  20. }
  21. hasWords();
  22. //添加按钮
  23. $(".addKeyWord i").on('click',function(){
  24. sessionStorage.removeItem("keyWord");
  25. window.location.href="/front/wx_dataExport/keyWordInput";
  26. });
  27. // 添加keyWords检查输入框内是否有文字,如果有才能点击添加按钮
  28. $('.addkeyWord input.enterOne').on('input', function() {
  29. if ($(this).val().length >= 100) {
  30. var s = $(this).val().slice(0,100)
  31. $(this).val(s)
  32. weui.toast('关键词不能超过100字', {
  33. duration: 2000,
  34. className: 'text-overflow100',
  35. callback: function(){ console.log('close') }
  36. });
  37. return
  38. }
  39. var buttonDOM = $(this).siblings()[1].children[0];
  40. if ($(this).val().length >= 1) {
  41. buttonDOM.style.opacity = 1;
  42. buttonDOM.removeAttribute("disabled");
  43. //
  44. $(this).next().find(".appended").prop("disabled", false);
  45. $(this).next().find(".exclude").prop("disabled", false);
  46. } else {
  47. buttonDOM.style.opacity = .5;
  48. buttonDOM.setAttribute("disabled", true);
  49. //
  50. $(this).next().find(".appended").prop("disabled", true);
  51. $(this).next().find(".exclude").prop("disabled", true);
  52. }
  53. });
  54. // $('.exclusion input.enterOne').on('input', function() {
  55. // var buttonDOM = $(this).siblings().find('button')[0]
  56. // if ($(this).val().length >= 1) {
  57. // buttonDOM.style.opacity = 1
  58. // buttonDOM.removeAttribute("disabled")
  59. // } else {
  60. // buttonDOM.style.opacity = .5
  61. // buttonDOM.setAttribute("disabled", true)
  62. // }
  63. // })
  64. // 添加 按钮的点击事件
  65. $('.addkeyWord .btn .save').on('click', function(){
  66. var keyWord = $('.addkeyWord input.enterOne').val();
  67. var Obj = {"keyWord": keyWord};
  68. keyWordArr.push(Obj);
  69. localStorage.setItem("keyWord", JSON.stringify(keyWordArr));
  70. var html = `<li>
  71. <div class="one">
  72. <div>
  73. <span>
  74. <strong> 关键词:</strong>
  75. <p class="key">${ keyWord }</p>
  76. </span>
  77. </div>
  78. <button class="editKeyWord"><i class="iconfont icon-xiugai"></i> 修改</button>
  79. </div>
  80. <div class="modify">
  81. <span contentEditable="true">${ keyWord }</span>
  82. <button class="addAdjunctWord">编辑 附加词</button>
  83. <button class="addExclusion">添加 排除词</button>
  84. <button class="deleteKey">删除</button>
  85. <button class="ascertainKey">确定</button>
  86. </div>
  87. </li>`
  88. $('.showKeyWord > ul').prepend(html);
  89. // 隐藏
  90. $(".addkeyWord").hide();
  91. $('.showKeyWord').show();
  92. $('.enter.addkeyWord > input').val('')
  93. var buttonDOM = $('.enter.addkeyWord .btn button')[0]
  94. buttonDOM.style.opacity = .5
  95. buttonDOM.setAttribute("disabled", true)
  96. })
  97. // 编辑
  98. $(".showKeyWord").on('click', '.editKeyWord',function(e){
  99. $('.modify span').each(function(item){
  100. $('.modify span')[0].contentEditable = true
  101. });
  102. // console.log($(this).parent().siblings().find('span').text())
  103. $(this).parent().hide();
  104. $(this).parent().siblings().css('display','block')
  105. });
  106. // 编辑删除
  107. $('.showKeyWord').on('click', '.deleteKey', function(e) {
  108. var jQueryDOM = $(this).parents('li');
  109. var keyWord = $(this).parent().prev().find('.key').text();
  110. weui.confirm('确定要删除关键词?', {
  111. buttons: [{
  112. label: '取消',
  113. type: 'default',
  114. onClick: function(){
  115. }
  116. }, {
  117. label: '确定',
  118. type: 'primary',
  119. onClick: function(){
  120. keyWordArr = keyWordArr.filter((e=>{return e.keyWord!==keyWord}));
  121. localStorage.keyWord = JSON.stringify(keyWordArr);
  122. jQueryDOM.remove();
  123. hasWords()
  124. }
  125. }]
  126. });
  127. // console.log('删除关键词:',$(this).parent().find('span').text())
  128. })
  129. // 编辑确定
  130. $('.showKeyWord').on('click', '.ascertainKey', function(e) {
  131. var $this = $(this);
  132. var keyWord = $(this).siblings('span').text();
  133. var keyWords = $(this).parent().prev().find('.key').text();
  134. for(var i in keyWordArr){
  135. if(keyWordArr[i].keyWord === keyWords){
  136. keyWordArr[i].keyWord = keyWord;
  137. }
  138. }
  139. localStorage.keyWord = JSON.stringify(keyWordArr);
  140. // weui.confirm('保存关键词?', {
  141. // buttons: [{
  142. // label: '我再想想',
  143. // type: 'default',
  144. // onClick: function(){
  145. // $this.parent().hide().siblings().show()
  146. // console.log('我再想想')
  147. // }
  148. // }, {
  149. // label: '确定保存',
  150. // type: 'primary',
  151. // onClick: function(){
  152. // $this.parent().siblings().find('.key').text(keyWord)
  153. // $this.parent().hide().siblings().show()
  154. // console.log(keyWord)
  155. // }
  156. // }]
  157. // });
  158. $this.parent().siblings().find('.key').text(keyWord)
  159. $this.parent().hide().siblings().show()
  160. // console.log(keyWord)
  161. })
  162. //去空格方法
  163. String.prototype.trim = function(){
  164. return this.replace(/(^\s*)|(\s*$)/g, ' ');
  165. }
  166. //关键词个数
  167. $('.modify span').keydown(function(){
  168. var olength = $(this).text().replace(/\s+/g,'').length;
  169. // console.log(olength)
  170. if(olength >= 100){
  171. $('.fontLength').show();
  172. var s = $(this).text().slice(0,100)
  173. $(this).text(s)
  174. weui.toast('关键词不能超过100字', {
  175. duration: 2000,
  176. className: 'text-overflow100',
  177. callback: function(){
  178. }
  179. });
  180. }
  181. })
  182. // $(".addExclusion").on('click',function(){
  183. // $('.exclusion').show();
  184. // $('.showKeyWord').hide();
  185. // })
  186. })