additionWord.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. $(function(){
  2. $('.knowBtn').on('click',function(){
  3. $(".problemPop").hide()
  4. })
  5. $(".problem").on('click',function(){
  6. $(".problemPop").css("display",'flex');
  7. })
  8. function hasWords () {
  9. var showKeyWordLength = $(".showKeyWord ul").find('li').length;
  10. if(showKeyWordLength === 0){
  11. $(".addkeyWord").show();
  12. $('.addKeyWord').hide();
  13. } else {
  14. $(".addkeyWord").hide();
  15. $('.addKeyWord').show();
  16. }
  17. }
  18. hasWords()
  19. // 添加按钮
  20. $(".addKeyWord i").on('click',function(){
  21. $(".addkeyWord").show();
  22. $(".addKeyWord").hide();
  23. $(".addkeyWord input").focus();
  24. $('.showKeyWord').find('.one').show();
  25. $('.showKeyWord').find('.modify').hide();
  26. })
  27. // 输入框自适应高度
  28. $('textarea').each(function(i,dom){
  29. // console.log(i,dom)
  30. dom.style.height = dom.scrollHeight +'px';
  31. })
  32. $("textarea").on("input", function() {
  33. this.style.height = 'auto';
  34. this.style.height = this.scrollHeight + "px";
  35. })
  36. // 添加keyWords检查输入框内是否有文字,如果有才能点击添加按钮
  37. $('.addkeyWord input.enterOne').on('input', function() {
  38. var buttonDOM = $(this).siblings().find('button')[1]
  39. if ($.trim($(this).val()).length >= 1) {
  40. buttonDOM.style.opacity = 1
  41. buttonDOM.removeAttribute("disabled")
  42. } else {
  43. buttonDOM.style.opacity = .5
  44. buttonDOM.setAttribute("disabled", true)
  45. }
  46. })
  47. // 添加 按钮的点击事件
  48. $('.addkeyWord .btn button:nth-child(2)').on('click', function(){
  49. var timestamp = new Date().getTime();//动态生成不同的id,因为id唯一不能重复,所以 用时间戳 代替 防止重复
  50. var keyWord = $.trim($('.addkeyWord input.enterOne').val());
  51. if(keyWord==""&&keyWord.length==0){
  52. weui.toast('附加词不能为空', {
  53. duration: 2000,
  54. className: 'custom-toast',
  55. callback: function () { console.log('close') }
  56. });
  57. return
  58. }
  59. _addindex = 0;
  60. if($(".showKeyWord li").length>0){
  61. _addindex = Number($(".showKeyWord li:first").find(".editKeyWord").attr("dataIndex")) + Number(1);
  62. }
  63. if(addkws_arr[keyWord]!=undefined){
  64. weui.toast('您设置的附加词已存在,请调整后再添加。', {
  65. duration: 2000,
  66. className: 'custom-toast',
  67. callback: function () { console.log('close') }
  68. });
  69. return
  70. }
  71. //保存新附加词
  72. _addkws = keyWord;
  73. saveSession("",_addindex);
  74. var html = `<li>
  75. <div class="one">
  76. <div>
  77. <span>
  78. <p class="key">${ keyWord }</p>
  79. </span>
  80. </div>
  81. <button class="editKeyWord" dataindex="`+_addindex+`"><i class="iconfont icon-xiugai"></i> 修改</button>
  82. </div>
  83. <div class="modify">
  84. <textarea name="" rows="1" placeholder="" maxlength="20">${ keyWord}</textarea>
  85. <button class="deleteKey">删除</button>
  86. <button class="ascertainKey">确定</button>
  87. </div>
  88. </li>`
  89. $('.showKeyWord > ul').prepend(html)
  90. // 隐藏
  91. $(".addkeyWord").hide();
  92. $('.showKeyWord').show();
  93. $('.addKeyWord').show();
  94. $('.enter.addkeyWord > input').val('')
  95. var buttonDOM = $('.enter.addkeyWord .btn button')[1]
  96. buttonDOM.style.opacity = .5
  97. buttonDOM.setAttribute("disabled", true)
  98. })
  99. // 编辑
  100. $(".showKeyWord").on('click', '.editKeyWord',function(e){
  101. _addindex = Number($(this).attr("dataindex"));
  102. $('.enter.addkeyWord').hide()
  103. $('.addKeyWord').hide()
  104. let oSpan = $(this).parent().siblings().children('textarea');
  105. let val = $(oSpan).val()
  106. $(this).parent().siblings().show().parents('li').siblings().children('.modify').hide().siblings('.one').show()
  107. $(oSpan).val('').focus().val(val)
  108. $(this).parent().hide()
  109. $(this).parent().siblings().css('display','block')
  110. })
  111. // 编辑 删除
  112. $('.showKeyWord').on('click', '.deleteKey', function(e) {
  113. var jQueryDOM = $(this).parents('li');
  114. _addkws = $.trim($(this).siblings('textarea').val());
  115. var _index = $('.showKeyWord li').length - $(this).parent().parent("li").index() -1;
  116. weui.confirm('确定要删除附加词?', {
  117. buttons: [{
  118. label: '取消',
  119. type: 'default',
  120. onClick: function () { console.log('不删了') }
  121. }, {
  122. label: '确定',
  123. type: 'primary',
  124. onClick: function () {
  125. jQueryDOM.remove();
  126. hasWords();
  127. saveSession("D",_addindex,_index);
  128. }
  129. }]
  130. });
  131. })
  132. // 编辑 确定
  133. $('.showKeyWord').on('click', '.ascertainKey', function(e) {
  134. var keyWord = $.trim($(this).siblings('textarea').val());
  135. var _index = $('.showKeyWord li').length - $(this).parent().parent("li").index() -1;
  136. if(keyWord.length==0){
  137. weui.toast('附加词不能为空', {
  138. duration: 2000,
  139. className: 'custom-toast',
  140. callback: function () { console.log('close') }
  141. });
  142. }else if(keyWord.length > 20){
  143. weui.toast('每组附加词不能超过20字', {
  144. duration: 2000,
  145. className: 'custom-toast',
  146. callback: function () { console.log('close') }
  147. });
  148. }else{
  149. if(addkws_arr[keyWord]!=undefined&&addkws_arr[keyWord]!=_addindex){
  150. weui.toast('您设置的附加词已存在,请调整后再添加。', {
  151. duration: 2000,
  152. className: 'custom-toast',
  153. callback: function () { console.log('close') }
  154. });
  155. return
  156. }
  157. $('.addKeyWord').show()
  158. _addkws = keyWord;
  159. saveSession("",_addindex,_index);
  160. $(this).parent().siblings().find('.key').text(keyWord)
  161. $(this).parent().hide().siblings().show()
  162. }
  163. })
  164. //防止键盘把当前输入框给挡住
  165. var u = navigator.userAgent;
  166. var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
  167. var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
  168. if(isAndroid){
  169. //防止键盘把当前输入框给挡住
  170. window.addEventListener('resize', function () {
  171.    if (document.activeElement.tagName == 'INPUT' || document.activeElement.tagName == 'TEXTAREA') {
  172.    window.setTimeout(function () {
  173.           document.activeElement.scrollIntoViewIfNeeded();
  174. }, 0);
  175. }
  176. })
  177. }
  178. })