Html
    Css
    Js
1
2
3
<div class="container">
<input id="abc" value="jQuery"><span id="wordCountShow"></span>
</div>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$.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(15, $("#wordCountShow"));
})
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
↑上面代码改变,会自动显示代码结果 jQuery调用版本:1.11.3
 立即下载

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

0