领星动网编程开发之爆炸小宇宙

cnitblog.com/lxasp - - 有一种信念叫做编程
posts - 61, comments - 34, trackbacks - 0, articles - 0

jQuery 插件代码编写模板 jQuery-Plugin-Template

Posted on 2009-08-22 14:23 lxasp 阅读(1322) 评论(1)  编辑 收藏 引用 所属分类: AJAX
jQuery 插件代码编写模板 jQuery-Plugin-Template 下载:
http://www.lxasp.com/dbbs/a/a.asp?B=101&ID=13

/*
@name: jQuery Plugin Template for Coding
*/
;(
function($) {//[--jQuery Plugin Container

//declare the plugin's version ; use to check the plugin exists
$.__plugin_name__ = $.__plugin_name__ || {version:'0.1.0'};

//[--Plugin Define
var __plugin_name__ = function(node,opts) {

    
var me=this,$me=$(this);
    
var $mine=$(node); //get the plugin's Operation jQuery DOM Element

    
//Public Methods
    $.extend(me, {
        show: 
function() {
            __show__();
        },
        hide: 
function() {
            __hide__();
        },
        options: 
function() {
            
//return the preset options to users code
            //let users can be change options by later code
            return opts;
        }
    });

    
//Private Variables ( Module Level )
    var m_var1, m_var2, m_var3;

    
//init the plugin
    function __init__(){
        alert('jQuery Plugin init');
        
if (opts.autoShow) __show__();
    }
    __init__();

    
//Private Functions
    function __show__(){
        
//function code
        $mine.text('Hello jQuery Plugin !');
        
//call the event
        opts.onShow(me,opts);
    }

    
function __hide__(){
        
//function code
        $mine.text('');
        
//call the event
        opts.onHide(me,opts);
    }


};
//--]Plugin Define


//jQuery Plugin Implementation
$.fn.__plugin_name__ = function(conf) {

    
//return existing instance // let users can use the Public Methods
    //Usage: var obj = $('#id').__plugin_name__({ <options> }).data("__plugin_name__");
    var el = this.eq(typeof conf == 'number' ? conf : 0).data("__plugin_name__");
    
if (el) { return el; }

    
//setup default options
    var opts = {
        autoShow:
true,
        onShow:
function(e,o){},
        onHide:
function(e,o){},
        api:
false
    };

    
//if no users options then use the default options
    $.extend(opts, conf);

    
// install the plugin for each items in jQuery
    this.each(function() {
        el 
= new __plugin_name__(this, opts);
        $(
this).data("__plugin_name__", el);
    });

    
//api=true let users can immediate use the Public Methods
    return opts.api ? el: this;

};


})(jQuery);
//--]jQuery Plugin Container

Feedback

# re: jQuery 插件代码编写模板 jQuery-Plugin-Template  回复  更多评论   

2009-08-22 21:54 by 凡客诚品
不错,正需要
只有注册用户登录后才能发表评论。