开发者

jQuery plugin default options prefix, string combine

开发者 https://www.devze.com 2023-04-07 13:17 出处:网络
I\'m wring a jQuery plugin, and I want the divs created inside of the plugin can be customized the ids, which i mean:

I'm wring a jQuery plugin, and I want the divs created inside of the plugin can be customized the ids, which i mean:

this is what i've done:

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

})(jQuery);

and i want this:

(functi开发者_开发知识库on($) {
    $.fn.plugin = function (options) {
        var defaults = {
            plugin_prefix = "nos-",
            box_id = _somethinghere_ + "box_id",
           shoes_id = _somethinghere_ + "shoes_id"
    }
    ...
    ...
    ...
   return this;

})(jQuery);

to make "somethinghere" equals to defaults.plugin_prefix(in this case, "nos").

can anybody help me figure this out?

Thx !


Don't build the id attributes until you have the prefix in hand. Leave plugin_prefix in your defaults but move box_id and shoes_id to somewhere else.

$.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...
    };
    //...
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号