"Build" JS variable
I'm having problem for a long time and I would like to solve it now. In php, if you want to retreive variable $var123, you could do it this way
$varname = "var123";
$var = $$varname;
H开发者_如何学运维ow could I do the same in js and/or jquery?
var varname = 'var123';
var value = window[varname];
If this variable in global scope, you can do something like:
window['variableName']
But i'm not sure that it's possible to implement this if you're not in global scope (in function body)
As long as the variable exists in your current scope global scope, you can access it using window.
var textVar = 'The text inside';
alert(window['textVar']);
精彩评论