如何选择数组中变量名的innerhtml?

huangapple go评论80阅读模式
英文:

How can I select the innerhtml of a variable name in an array?

问题

如何使secondhtmlelement的innerHTML显示为"Hello World"?

我尝试了类似上面的代码,当我按下某些东西时,它应该根据我想要的数组条目来更改不同元素的innerHTML。

英文:

So if I have this code:

const firsthtmlelement = document.getElementById("firsthtmlelement");
const secondhtmlelement = document.getElementById("secondhtmlelement");

const array = ['firsthtmlelement', 'secondhtmlelement']

array[1].innerHTML = "Hello World";

How can I make the secondhtmlelement's innerHTML show up as "Hello World"?

I tried something similar to the code above, when I press something then it is supposed to change the innerHTML of a different element depending on which entry from an array I wanted.

答案1

得分: 1

尝试在你的 array 中添加实际元素,而不仅仅是字符串名称:

let firsthtmlelement = document.getElementById("firsthtmlelement");
let secondhtmlelement = document.getElementById("secondhtmlelement");

const button = document.getElementById("button");

const array = [firsthtmlelement, secondhtmlelement];

button.addEventListener("click", function() {
  array[1].innerHTML = "Hello World";
});
<div id="firsthtmlelement">div1</div>
<div id="secondhtmlelement">div2</div>
<button id="button">press</button>
英文:

Try to add actual elements in your array instead of just string names:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let firsthtmlelement = document.getElementById(&quot;firsthtmlelement&quot;);
let secondhtmlelement = document.getElementById(&quot;secondhtmlelement&quot;);

const button = document.getElementById(&quot;button&quot;);

const array = [firsthtmlelement, secondhtmlelement];


button.addEventListener(&quot;click&quot;, function() {
  array[1].innerHTML = &quot;Hello World&quot;;
});

<!-- language: lang-html -->

&lt;div id=&quot;firsthtmlelement&quot;&gt;div1&lt;/div&gt;
&lt;div id=&quot;secondhtmlelement&quot;&gt;div2&lt;/div&gt;
&lt;button id=&quot;button&quot;&gt;press&lt;/button&gt;

<!-- end snippet -->

答案2

得分: 0

如果你的变量是全局的,尝试使用以下方式:

window["variableName"].innerHTML = myValue;
let firsthtmlelement = document.getElementById("firsthtmlelement");
let secondhtmlelement = document.getElementById("secondhtmlelement");
const button = document.getElementById("button");

const array = ['firsthtmlelement', 'secondhtmlelement'];

button.addEventListener("click", function() {
  console.log(array[1]);
  // 在这个示例中,我们将硬编码要更改的元素
  window[array[1]].innerHTML = "Hello World";
});
<div id="firsthtmlelement">div1</div>
<div id="secondhtmlelement">div2</div>
<button id="button">press</button>
英文:

if you variable is a global try using this window[&quot;variableName&quot;].innerHTML = myValue;

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let firsthtmlelement = document.getElementById(&quot;firsthtmlelement&quot;);
let secondhtmlelement = document.getElementById(&quot;secondhtmlelement&quot;);
const button = document.getElementById(&quot;button&quot;);

const array = [&#39;firsthtmlelement&#39;, &#39;secondhtmlelement&#39;];


button.addEventListener(&quot;click&quot;, function() {
  console.log(array[1]);
  //in this example we will just hardcode which element to change
  window[array[1]].innerHTML = &quot;Hello World&quot;;
});

<!-- language: lang-html -->

&lt;div id=&quot;firsthtmlelement&quot;&gt;div1&lt;/div&gt;
&lt;div id=&quot;secondhtmlelement&quot;&gt;div2&lt;/div&gt;
&lt;button id=&quot;button&quot;&gt;press&lt;/button&gt;

<!-- end snippet -->

答案3

得分: 0

当你调用 arry[1] 时,你会得到一个字符串,而不是变量。

有很多方法可以实现动态变量。

下面我有两种解决方案:

第一个解决方案创建一个对象,其中键是名称,值是该元素的 querySelector。

第二种是使用类似你的示例的数组。

const elementsObject = {
  "firsthtmlelement": document.querySelector("#firsthtmlelement"),
  "secondhtmlelement": document.querySelector("#secondhtmlelement")
};

elementsObject["firsthtmlelement"].innerHTML = "Hello World 1";

const elementsArray = [document.querySelector("#firsthtmlelement"), document.querySelector("#secondhtmlelement")];

elementsArray[1].innerHTML = "Hello World 2";
<div id="firsthtmlelement"></div>
<div id="secondhtmlelement"></div>
英文:

When you call arry[1] you are getting a string, and not the variable.

There are many ways to accomplish dynamic variables.

Below I have two solutions:

The first solution creates an object where the key is the name and the value is the querySelector for that element.

The second is using an array like your example.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const elementsObject = {
  &quot;firsthtmlelement&quot;: document.querySelector(&quot;#firsthtmlelement&quot;),
  &quot;secondhtmlelement&quot;: document.querySelector(&quot;#secondhtmlelement&quot;)
};

elementsObject[&quot;firsthtmlelement&quot;].innerHTML = &quot;Hello World 1&quot;;

const elementsArray = [document.querySelector(&quot;#firsthtmlelement&quot;), document.querySelector(&quot;#secondhtmlelement&quot;)];

elementsArray[1].innerHTML = &quot;Hello World 2&quot;;

<!-- language: lang-html -->

&lt;div id=&quot;firsthtmlelement&quot;&gt;&lt;/div&gt;
&lt;div id=&quot;secondhtmlelement&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月29日 05:09:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76576727.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定