jQuery插件默认选项前缀,字符串组合

时间:2011-09-23 06:06:04

标签: jquery plugins

我正在拧一个jQuery插件,我想在插件里面创建的div可以自定义id,我的意思是:

这就是我所做的:

(function($) {
    $.fn.plugin = function (options) {
        var defaults = {
            box_id = "nos-box_id",
           shoes_id = "nos-shoes_id"
    }
    ...
    ...
    ...
   return this;

})(jQuery);

我想要这个:

(function($) {
    $.fn.plugin = function (options) {
        var defaults = {
            plugin_prefix = "nos-",
            box_id = _somethinghere_ + "box_id",
           shoes_id = _somethinghere_ + "shoes_id"
    }
    ...
    ...
    ...
   return this;

})(jQuery);

使“某事”等于defaults.plugin_prefix(在本例中为“nos”)。

任何人都可以帮我解决这个问题吗?

Thx!

1 个答案:

答案 0 :(得分:1)

在手头有前缀之前,不要构建id属性。将plugin_prefix保留为默认值,但将box_idshoes_id移至其他位置。

$.fn.plugin = function (options) {
    var defaults = {
        plugin_prefix: "nos-",
        // other options ...
    };
    options = $.extend({ }, options, defaults);

    var ids = {
        box_id:      options.plugin_prefix + "box_id",
        shoes_id:    options.plugin_prefix + "shoes_id",
        pancakes_id: options.plugin_prefix + "pancakes_id",
        // other ids you may need...
    };
    //...