JavaScript:如何按名称获取变量的值
我有 JS 文件:
var text1 = "There is text1";
var text2 = "There is text2";
var text3 = "There is text3";
在变量 actual_text 中,我在 str 中有“text2”。
actual_text = "text2";
它是字符串,因为它基于前面的代码,这是正确的。
现在我需要将 actual_text 中的“text2”重新输入到 text2(作为带有内容的变量)并使用变量 actual_text 将其输出:
<p></p>
document.getElementById('myoutput').innerHTML=actual_text;
回答
尝试这个:
var text1 = "There is text1";
var text2 = "There is text2";
var text3 = "There is text3";
actual_text = "text2";
document.getElementById('myoutput').innerHTML = this[actual_text];
<p></p>