additionWord.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. //保存新附加词
  50. _addkws = keyWord;
  51. saveSession();
  52. var html = `<li>
  53. <div class="one">
  54. <div>
  55. <span>
  56. <p class="key">${ keyWord }</p>
  57. </span>
  58. </div>
  59. <button class="editKeyWord"><i class="iconfont icon-xiugai"></i> 修改</button>
  60. </div>
  61. <div class="modify">
  62. <textarea name="" rows="1" placeholder="" maxlength="20">${ keyWord}</textarea>
  63. <button class="deleteKey">删除</button>
  64. <button class="ascertainKey">确定</button>
  65. </div>
  66. </li>`
  67. $('.showKeyWord > ul').prepend(html)
  68. // 隐藏
  69. $(".addkeyWord").hide();
  70. $('.showKeyWord').show();
  71. $('.enter.addkeyWord > input').val('')
  72. var buttonDOM = $('.enter.addkeyWord .btn button')[0]
  73. buttonDOM.style.opacity = .5
  74. buttonDOM.setAttribute("disabled", true)
  75. })
  76. // 编辑
  77. $(".showKeyWord").on('click', '.editKeyWord',function(e){
  78. $('.enter.addkeyWord').hide()
  79. $('.addKeyWord').hide()
  80. let oSpan = $(this).parent().siblings().children('textarea');
  81. let val = $(oSpan).val()
  82. $(this).parent().siblings().show().parents('li').siblings().children('.modify').hide().siblings('.one').show()
  83. $(oSpan).val('').focus().val(val)
  84. $(this).parent().hide()
  85. $(this).parent().siblings().css('display','block')
  86. })
  87. // 编辑 删除
  88. $('.showKeyWord').on('click', '.deleteKey', function(e) {
  89. var jQueryDOM = $(this).parents('li');
  90. _addkws = $(this).siblings('textarea').val();
  91. _addindex = $(".showKeyWord li").length - $(this).parent().parent('li').index() - 1;
  92. weui.confirm('确定要删除附加词?', {
  93. buttons: [{
  94. label: '取消',
  95. type: 'default',
  96. onClick: function () { console.log('不删了') }
  97. }, {
  98. label: '确定',
  99. type: 'primary',
  100. onClick: function () {
  101. jQueryDOM.remove();
  102. hasWords();
  103. $('.addKeyWord').show();
  104. saveSession("D");
  105. }
  106. }]
  107. });
  108. })
  109. // 编辑 确定
  110. $('.showKeyWord').on('click', '.ascertainKey', function(e) {
  111. $('.addKeyWord').show()
  112. var keyWord = $(this).siblings('textarea').val();
  113. if(keyWord.length > 20){
  114. weui.toast('每组附加词不能超过20字', {
  115. duration: 2000,
  116. className: 'custom-toast',
  117. callback: function () { console.log('close') }
  118. });
  119. }else{
  120. _addindex = $(".showKeyWord li").length - $(this).parent().parent('li').index() - 1;
  121. _addkws = keyWord;
  122. saveSession();
  123. $(this).parent().siblings().find('.key').text(keyWord)
  124. $(this).parent().hide().siblings().show()
  125. }
  126. })
  127. //防止键盘把当前输入框给挡住
  128. $('input[type="text"],textarea').focus(function () {
  129. var target = this;
  130. setTimeout(function(){
  131. target.scrollIntoViewIfNeeded();
  132. },400);
  133. });
  134. })