所以我有下面這段代碼:如何從JavaScript中的對象鍵值返回數組?
var structures = {
loginStructure : function(){
return structure = [
'<form name="',opts.formClass,'" class="',opts.formClass,'" method="post" action="#">',
'<fieldset class="',opts.fieldsWrapper,'">',
'<fieldset class="',opts.userWrapper,'">',
'<label for="',opts.userInt,'" class="',opts.userLbl,'"><img src="',opts.userIcon,'" alt="',opts.userName,'" /></label>',
'<input type="text" name="',opts.userInt,'" class="',opts.userInt,'" placeholder="',checkNameLenght(opts.userName,namesLenght.userNameLenght,16,'Username'),'" value="" autocomplete="off" />',
'</fieldset>',
'<fieldset class="',opts.passWrapper,'">',
'<label for="',opts.passInt,'" class="',opts.passLbl,'"><img src="',opts.passIcon,'" alt="',opts.passName,'" /></label>',
'<input type="password" name="',opts.passInt,'" class="',opts.passInt,'" placeholder="',checkNameLenght(opts.passName,namesLenght.passNameLenght,16,'Password'),'" value="" autocomplete="off" />',
'</fieldset>',
'<fieldset class="',opts.btnWrapper,'">',
'<button type="submit" name="',opts.btnInt,'" class="',opts.btnInt,'">',checkNameLenght(opts.btnName,namesLenght.btnNameLenght,7,'Login'),'</button>',
'</fieldset>',
'</fieldset>',
'<div class="toogle-button">',
'<ul class="inside">',
'<li class="toogle"><a><img src="assets/gfx/toogle.png" alt="Back" /></a></li>',
'</ul>',
'</div>',
'</form>',
'<div class="toogle-buttons">',
'</div>'
];
}
}
這將返回(如果我做console.log(structures.loginStructure)
)只是一個function()
。有沒有一種方法可以讓它返回我在那裏的實際數組?
它的目的是將多個數組定義爲對象鍵,如果有可能返回這樣的東西,因爲它看起來更容易。還是有更好的方法來做到這一點?
順便說一句,這與jQuery無關 - 但將其標記爲表示您可能正在使用jQuery--這可能會提示其他人提供jQuery相關答案。 – ziesemer 2012-01-14 20:19:10
是的,我也使用jQuery,實際上這段代碼是jQuery插件的一部分。 – Roland 2012-01-14 20:25:48
如果所有變量都被正確定義,似乎在這裏工作得很好:http://jsfiddle.net/jfriend00/ekZVE/。 – jfriend00 2012-01-14 20:53:00