jquery-简单插件编写
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript" src="jquery-1.8.3.js"></script>
<style>
div{
width:50px;
height:50px;
display:block;
}
#div1{
color:#00F;
}
#div2{
color:#F00;
}
#div3{
color:#0F0;
}
</style>
</head>
<body>
<div id="div1">1</div>
<div id="div2">2</div>
<div id="div3">3</div>
<script type="text/javascript">
//插件的简单例子:
//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<$('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<$('div').length; i++){
$('div').eq(i).mouseover(function(){
alert($.col($(this)))
})
}
</script>
</body>
</html>