additionWord.js 6.4 KB

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