谁能告诉我如何使这个 jQuery 插件可链接?该插件将文本输入限制在字段中,如果传入,则将剩余文本计数返回给第二个参数。谢谢。
(function($) {
$.fn.extend({
limit: function(limit, element) {
var interval;
var self=$(this);
$(this).focus(function(){
interval = window.setInterval(function(){
substring(self, limit, element);
},100)
});
$(this).blur(function(){
clearInterval(interval);
substring(self, limit, element);
});
substring(self, limit, element);
}
});
function substring(self, limit, element) {
var val = $(self).val();
var length = val ? val.length : 0 ;
if( length > limit ) {
$(self).val($(self).val().substring(0,limit));
}
var toe = typeof element;
if (toe!='undefined') {
if (toe=='string') {
$(element).html((limit-length<=0)?'0':limit-length);
} else if (toe=='object') {
element.html((limit-length<=0)?'0':limit-length);
}
}
}
})(jQuery);