這篇文章主要介紹了jQuery實現限制textarea文本框輸入字符數量的方法,涉及jQuery鍵盤事件及頁面元素的相關操作技巧,需要的朋友可以參考下
本文實例講述了jQuery實現限制textarea文本框輸入字符數量的方法。分享給大家供大家參考。具體實現方法如下:
- (function($) {
- $.fn.extend( {
- limiter: function(limit, elem) {
- $(this).on("keyup focus", function() {
- setCount(this, elem);
- });
- function setCount(src, elem) {
- var chars = src.value.length;
- if (chars > limit) {
- src.value = src.value.substr(0, limit);
- chars = limit;
- }
- elem.html( limit - chars );
- }
- setCount($(this)[0], elem);
- }
- });
- })(jQuery);
- //To setup the limiter, simply include a call similar to the one below:
- var elem = $("#chars");
- $("#text").limiter(100, elem);
希望本文所述對大家的jQuery程序設計有所幫助。
新聞熱點
疑難解答
圖片精選