---
title: "jquery-简单插件编写"
author: "keimon"
date: "2013-03-21T14:16:37Z"
tags: []
categories: ["Web前端"]
slug: "jquery"
draft: false
lastmod: "2013-03-21T14:16:37Z"
id: 1833366
url: "https://keimon.iteye.com/blog/1833366"
---

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd](http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd)"&gt;  
&lt;html xmlns="[http://www.w3.org/1999/xhtml](http://www.w3.org/1999/xhtml)"&gt;  
&lt;head&gt;  
&lt;meta http-equiv="Content-Type" content="text/html; charset=gb2312" /&gt;  
&lt;title&gt;无标题文档&lt;/title&gt;  
&lt;script type="text/javascript" src="jquery-1.8.3.js"&gt;&lt;/script&gt;  
&lt;style&gt;  
div{  
  width:50px;  
  height:50px;  
  display:block;  
}  
#div1{  
  color:#00F;  
}  
#div2{  
  color:#F00;  
}  
#div3{  
  color:#0F0;  
}  
&lt;/style&gt;  
&lt;/head&gt;

&lt;body&gt;  
&lt;div id="div1"&gt;1&lt;/div&gt;  
&lt;div id="div2"&gt;2&lt;/div&gt;  
&lt;div id="div3"&gt;3&lt;/div&gt;  
&lt;script type="text/javascript"&gt;  
//插件的简单例子：  
//1、插件的对象方法；  
/*  
;(function($){  
   $.fn.extend({  
      color:function(value){  
      if(value==''|| value==undefined){  
      return this.css('color');  
   }else{  
         return this.css('color',value);  
   }  
   }  
   })  
})(jQuery);

for(var i=0; i&lt;$('div').length; i++){  
  $('div').eq(i).mouseover(function(){  
     alert($(this).color())  
  })  
}  
\*/

//2、插件的全局函数；  
;(function($){  
   $.extend({  
     col:function(obj){  
     return obj.css('color');  
  }  
   })  
})(jQuery)

for(var i=0; i&lt;$('div').length; i++){  
  $('div').eq(i).mouseover(function(){  
     alert($.col($(this)))  
  })  
}  
&lt;/script&gt;  
&lt;/body&gt;  
&lt;/html&gt;