javascript - 当我的字符数达到特定数字时使用 jQuery 更改文本

标签 javascript jquery css twitter-bootstrap

我为我的文本区域制作了一个单词计数器。我一直在尝试更改 jQuery,以便它在达到特定数量的字符时显示不同的警报,但我未能实现这一点。例如,我想将文字变成红色并在 60 个字符处说“这将变成两条 SMS 消息”。我该怎么做?

我该怎么做?

$(function() {
    var sms_max = 99;
    $('#sms_feedback').html(sms_max + ' characters remaining');

    $('#sms').keyup(function () {
        var text_length = $('#sms').val().length;
        var text_remaining = sms_max - text_length;

        $('#sms_feedback').html(text_remaining + ' characters remaining');
    });
})
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>



<button type="button" class="btn btn-link" style="margin-left: -9px; margin-bottom: 3px;" data-toggle="modal" data-target="#exampleModal">add one here</button>

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
  <div class="container-fluid">
    <div class="row">
      <div class="col-md-7">
        <textarea id="sms" style="height: 200px; width: 352px;" placeholder="Hello {clientName}."></textarea>
        <div id="sms_feedback"></div>
      </div>
	  </div></div> 
      </div>
    </div>
  </div>
</div>

最佳答案

您只需要在 text_length 上添加一个比较,并在输入 60 个字符时再执行一行以附加额外的文本并更改字体颜色。请参阅代码中的注释。

$(function() {
  var sms_max = 99;
  $('#sms_feedback').html(sms_max + ' characters remaining');

  $('#sms').keyup(function () {
    var text_length = $('#sms').val().length;
    var text_remaining = sms_max - text_length;

    // Set the color to black at first.
    $('#sms_feedback').html(text_remaining + ' characters remaining.').css({"color":"black"});;
    
    // At 60 chars or more, add text and change the color to red.
    if(text_length >= 60){
      $('#sms_feedback').append(" This will turn into two SMS messages.").css({"color":"red"});
    }
  });
})
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>



<button type="button" class="btn btn-link" style="margin-left: -9px; margin-bottom: 3px;" data-toggle="modal" data-target="#exampleModal">add one here</button>

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
  <div class="container-fluid">
    <div class="row">
      <div class="col-md-7">
        <textarea id="sms" style="height: 200px; width: 352px;" placeholder="Hello {clientName}."></textarea>
        <div id="sms_feedback"></div>
      </div>
	  </div></div> 
      </div>
    </div>
  </div>
</div>

关于javascript - 当我的字符数达到特定数字时使用 jQuery 更改文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49479041/

相关文章:

javascript - 函数 js 中的函数

javascript - 获取对象的复杂属性值

php - 从 View 表单到 PHP Controller 的 jquery post 未发布

javascript - 如何区分通过鼠标滚动和在 JavaScript 中以编程方式滚动?

javascript - 如何使用内联样式在滚动时使用变换 : rotate() with react. js

javascript - 将自定义 css 保持在 Angular 范围内

javascript - 检测 Canvas 边缘并允许远离它的移动

jquery scrollto + addclass 到 div

html - CSS div 不向左浮动,样式冲突?

javascript - 从多个相同文本中查找选定的文本