2011-09-23 24 views
1

我擰一個jQuery插件,和我想要的插件內部創建的div可定製的ID,我的意思是:jQuery插件默認選項前綴字符串結合

這是我做了什麼:

(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); 

使 「somethinghere」 等於defaults.plugin_prefix(在這種情況下, 「號」)。

任何人都可以幫我解決這個問題嗎?

Thx!

回答

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

謝謝!真的有幫助! –