JavaScript:如何按名称获取变量的值

dav*_*751 -1 javascript

我有 JS 文件:

var text1 = "There is text1";
var text2 = "There is text2";
var text3 = "There is text3";
Run Code Online (Sandbox Code Playgroud)

在变量 actual_text 中,我在 str 中有“text2”。

actual_text = "text2";
Run Code Online (Sandbox Code Playgroud)

它是字符串,因为它基于前面的代码,这是正确的。

现在我需要将 actual_text 中的“text2”重新输入到 text2(作为带有内容的变量)并使用变量 actual_text 将其输出:

<p id="myoutput"></p>
document.getElementById('myoutput').innerHTML=actual_text;
Run Code Online (Sandbox Code Playgroud)

Maj*_*awi 6

尝试这个:

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];
Run Code Online (Sandbox Code Playgroud)
<p id="myoutput"></p>
Run Code Online (Sandbox Code Playgroud)