Character countdown like on twitter

后端 未结 9 926
栀梦
栀梦 2020-12-02 06:54

How can I make a \"remaining characters\" countdown like the one on Twitter with jQuery? And also limit the input to a textarea.

9条回答
  •  春和景丽
    2020-12-02 07:29

    use jquery

    save this as jquery.textlimiter.js

    (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);
    

    usage

    
        
    100

提交回复
热议问题