自己开发的一个类似微博客输入框的一个jQuery插件,也可以让大家学习下怎么开发一个简单的jQuery插件。

(function($){
	$.fn.extend({
		speaker:function(countId){
		var intervalId = jQuery.data(this, 'intervalId');
		this.focus(function(){
			if(intervalId==null||intervalId!=0){
				intervalId = setInterval("jQuery.speaker.check('"+this.id+"','"+countId+"')",50);
				jQuery.data(this, 'intervalId', intervalId);
			}
		});
		this.blur(function(){
			var intervalId = jQuery.data(this, 'intervalId');
			jQuery.data(this, 'intervalId', 0);
			clearInterval(intervalId);
		});
		}
	});
	jQuery.extend({
		speaker: {
			check:function(id,countId){
				var str = $("#"+id).val();
				var count = 140-str.length;
				$("#"+countId).html(count);
				if(count>10){
					$("#"+countId).attr("style","color:#000000;");
				}else if(count>=0){
					$("#"+countId).attr("style","color:#085879;");
				}else{
					$("#"+countId).attr("style","color:#BA2636;");
				}
			}
		}
	});
})(jQuery);