jQuery实现简易的输入框字数计数功能示例

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

jQuery实现简易的输入框字数计数功能示例

ISaiSai   2020-05-15 我要评论

本文实例讲述了jQuery实现简易的输入框字数计数功能。分享给大家供大家参考,具体如下:

运行效果图如下:

具体代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title></title>
  <script src="jquery-1.7.2.min.js"></script>
</head>
<body>
<div class="container">
  <input id="abc" value=""><span id="wordCountShow"></span>
</div>
<script>
  $.fn.extend({
    wordCount: function (maxLength, wordWrapper) {
      var self = this;
      $(self).attr("maxlength", maxLength);
      showWordCount();
      $(this).on("input propertychange", showWordCount);
      function showWordCount() {
        curLength = $(self).val().length;
        var leaveCount = maxLength - curLength;
        wordWrapper.text(leaveCount + "/" + maxLength);
      }
    }
  })
  $(function () {
    $("#abc").wordCount(10, $("#wordCountShow"));
  })
</script>
</body>
</html>

PS:这里再为大家提供几款带有字数统计功能的在线工具供大家参考使用:

在线字数统计工具:
http://tools.softyun.net/code/zishutongji

在线文本美化排版工具:
http://tools.softyun.net/aideddesign/txt_paiban

在线自动排版与转换工具:
http://tools.softyun.net/aideddesign/txt_beaut

希望本文所述对大家jQuery程序设计有所帮助。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们