读书人

jquery-简略插件编写

发布时间: 2013-03-25 15:43:04 作者: rapoo

jquery-简单插件编写
<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>

读书人网 >Web前端

热点推荐