jQuery plugin default options prefix, string combine
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...
};
//...
精彩评论