支持默认选项

你可以通过接受选项来自定义插件。

$.fn.colourize = function(options) {

    // This is one method to support default options
    var style = $.extend({
        color: "green",
        backgroundColor: "white"
    }, options);

    // Set the colours on the current selection based on the option parameters
    return this.css({
        color: style.color,
        backgroundColor: style.backgroundColor
    });
};

用法示例:

$("button").colourize({
    color: "orange"
});

颜色选项绿色的默认值被 $.extend() 覆盖为橙色