支援預設選項

你可以通過接受選項來自定義外掛。

$.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() 覆蓋為橙色