- html
- 0/15
- CSS
- .textareaBox { position: relative; }
- .wordsNum { position: absolute; bottom: 0.83rem; right: 0.83rem; color: #B3B3B3; font-size: 1rem; }
- js
- // 封装一个限制字数方法
- var checkStrLengths = function (str, maxLength) {
- var maxLength = maxLength;
- var result = 0;
- if (str && str.length> maxLength) {
- result = maxLength;
- } else {
- result = str.length;
- } return result; }
- // 监听输入 $(".wishContent").on('input propertychange', function () { // 获取输入内容 var userDesc = $(this).val();
- // 判断字数 var len; if (userDesc) { len = checkStrLengths(userDesc, 15); } else { len = 0 }
- // 显示字数 $(".wordsNum").html(len + '/15'); });
来源: https://www.2cto.com/kf/201808/774775.html