2011-01-08 51 views
0

我需要調用一個使用var來工作的插件。但我無法得到可以改變的變種原因......這就是我正在嘗試做的事情。需要在Javascript中創建變量變量 - 不是數組

var selection = $(".view-your-rescues .cms_textarea,.post-a-rescue .cms_textarea,.edit-account-details .cms_textarea"); 

if(selection.length > 0) { 
    selection 
     .css({"height":"100","width":"500"}) 
     .each(function(i) { 
      var eval("hb"+i) = $(this).htmlbox({ 
       toolbars:[[ 
        "separator","bold","italic","underline","strike","sup","sub", 
        "separator","justify","left","center","right", 
        "separator","ol","ul","indent","outdent" 
       ]], 
       icons:"silk", 
       skin:"red", 
       idir:"/uploads/htmlbox/", 
       about:false 
     }); 
    }); 
} 

所以這是基於這個http://htmlbox.remiya.com/cms/demo/iconsets-demo/在那裏你會看到,爲了做兩件你需要有不同的增值經銷商。

的重要組成部分,是

.each(function(i){ 
    var eval("hb"+i)= $(this).htmlbox({ 

我試過EVAL ..但我不喜歡這個主意,它不反正工作...任何想法?

編輯:我不能做無功的eval( 「HB」 + I)= $(本),如果我去

的eval( 「HB」 + I)= $(本)

我剛剛得到「hb0未定義」

希望是有道理的。感謝您的幫助。 乾杯-Jeremy

回答

2

var不會有太大幫助在所有 - 即使它的工作,它會把這些變量的任何有用的範圍,在這種情況下。

全局實際上是通常的window成員,因此,你可以做的,如果你確定與全局,是一樣的東西

.each(function(i) { 
    window["hb"+i] = $(this).htmlbox({ /* your params */ }); 
}); 
+0

+1這是去 – Alex 2011-01-08 01:23:37