這篇文章主要介紹了jQuery實(shí)現(xiàn)限制textarea文本框輸入字符數(shù)量的方法,涉及jQuery鍵盤事件及頁(yè)面元素的相關(guān)操作技巧,需要的朋友可以參考下
本文實(shí)例講述了jQuery實(shí)現(xiàn)限制textarea文本框輸入字符數(shù)量的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
- (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);
希望本文所述對(duì)大家的jQuery程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選