添加EmojiOne Area后,字符计数器会中断

时间:2017-03-09 19:01:24

标签: javascript jquery

我的textarea有一个字符计数器,在我添加EmojiOne Area之前效果很好。表情符号选择器现在效果很好,但我的角色计数器停止工作。出于某种原因,keyup不再使用ID。

这是我目前的代码:

HTML

<textarea id="message" class="form-control" placeholder="Enter text here..."></textarea> 
<label><span id="chars" class="lead">140</span></label> characters left

的JavaScript

/*emojioneArea */
$(document).ready(function() {
  $("#message").emojioneArea({
    pickerPosition: "bottom",
    tonesStyle: "bullet"
  });
});

/*Character Counter */
function countChar(val) {
  var len = val.value.length;

  if (len >= 140) {
    val.value = val.value.substring(0, 140);
    $('#stat span').text(0);
  } else {
    $('#stat span').text(140 - len);
  }
}
countChar($('#message').get(0));
$('#message').keyup(function() {
  countChar(this);
});

/*emojioneArea */
$(document).ready(function() {
  $("#message").emojioneArea({
    pickerPosition: "bottom",
    tonesStyle: "bullet"
  });
});

/*Character Counter */
function countChar(val) {
  var len = val.value.length;

  if (len >= 140) {
    val.value = val.value.substring(0, 140);
    $('#stat span').text(0);
  } else {
    $('#stat span').text(140 - len);
  }
}
countChar($('#message').get(0));
$('#message').keyup(function() {
  countChar(this);
});
.emojionearea-editor:not(.inline) {
  min-height: 8em!important;
}

.emojionearea,
.emojionearea.form-control {
  display: block;
  position: relative !important;
  width: 100%;
  height: auto;
  padding: 0;
  font-size: 20px;
  border: 0;
  background-color: #fff;
  border: 1px solid #ccc;
  -moz-border-radius: 7px;
  -webkit-border-radius: 7px;
  border-radius: 7px;
  -moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
  -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
  box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
  -moz-transition: border-color 0.15s ease-in-out, -moz-box-shadow 0.15s ease-in-out;
  -o-transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
  -webkit-transition: border-color 0.15s ease-in-out, -webkit-box-shadow 0.15s ease-in-out;
  transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
}

.emojionearea .emojionearea-editor {
  display: block;
  height: auto;
  overflow: auto;
  font-size: inherit;
  color: #59A80B;
  cursor: text;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="https://cdn.rawgit.com/mervick/emojionearea/master/dist/emojionearea.min.js"></script>
<link href="https://cdn.rawgit.com/mervick/emojionearea/master/dist/emojionearea.min.css" rel="stylesheet">

<textarea id="message" class="form-control" placeholder="Enter text here..."></textarea>
<label><span id="chars" class="lead">140</span></label> characters left

或在JSFiddle中查看。

1 个答案:

答案 0 :(得分:3)

我发现你的代码实际上存在一些问题。以下是使用emojionearea和字符数的更新工作代码。

/*emojioneArea */
   $(document).ready(function() {
         $("#message").emojioneArea({
                       pickerPosition: "bottom",
                       tonesStyle: "bullet",
                       events: {
                         keyup: function (editor, event) {
                           console.log('event:keyup');
                           countChar(this);
                        }
                      }
               });
    }); 

   /*Character Counter */
        function countChar(val) {
            var len = val.getText().length;
            if (len >= 140) {
                  val.value = val.content.substring(0, 140);
                  $('#chars').text(0);
            } else {
                 $('#chars').text(140 - len);
            }
        }

这是相同的小提琴链接,您可以在其中进行检查。 Fiddle

使用初始加载Fiddle

更新了代码