导航首页 » 技术教程 » jQuery实现简易的输入框字数计数功能示例
jQuery实现简易的输入框字数计数功能示例 108 2024-02-05   

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

运行效果图如下:

查看图片

具体代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title></title>
  <script src="http://www.gimoo.net/t/1807/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.gimoo.net/code/zishutongji

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

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

更多关于jQuery相关内容感兴趣的读者可查看本站专题:《jQuery常用插件及用法总结》、《jQuery扩展技巧总结》、《jQuery切换特效与技巧总结》、《jQuery遍历算法与技巧总结》、《jQuery常见经典特效汇总》、《jQuery动画与特效用法总结》及《jquery选择器用法总结》

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



!!!站长长期在线接!!!

网站、小程序:定制开发/二次开发/仿制开发等

各种疑难杂症解决/定制接口/定制采集等

站长微信:lxwl520520

站长QQ:1737366103