学习 准备 尝试 谨慎小心

0%

JavaScript和Jquery插件开发指南

参考博客 jQuery插件开发精品教程,让你的jQuery提升一个台阶

1
// 自调用匿名空间(避免代码命名冲突)
2
;(function(window, document, undefined) {
3
    //定义Beautifier的构造函数
4
    var Beautifier = function(ele, opt) {
5
        this.$element = ele,
6
        this.defaults = {
7
            'color': 'red',
8
            'fontSize': '12px',
9
            'textDecoration': 'none'
10
        },
11
        this.options = $.extend({}, this.defaults, opt)
12
    }
13
    //定义Beautifier的方法
14
    Beautifier.prototype = {
15
        beautify: function() {
16
            return this.$element.css({
17
                'color': this.options.color,
18
                'fontSize': this.options.fontSize,
19
                'textDecoration': this.options.textDecoration
20
            });
21
        }
22
    }
23
    //在插件中使用Beautifier对象
24
    window.myPlugin = function(options) {
25
        //创建Beautifier的实体
26
        var beautifier = new Beautifier(this, options);
27
        //调用其方法
28
        return beautifier.beautify();
29
    }
30
})(jQuery, window, document);