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

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

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

问题

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

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

英文:

So if I have this code:

  1. const firsthtmlelement = document.getElementById("firsthtmlelement");
  2. const secondhtmlelement = document.getElementById("secondhtmlelement");
  3. const array = ['firsthtmlelement', 'secondhtmlelement']
  4. 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 中添加实际元素,而不仅仅是字符串名称:

  1. let firsthtmlelement = document.getElementById("firsthtmlelement");
  2. let secondhtmlelement = document.getElementById("secondhtmlelement");
  3. const button = document.getElementById("button");
  4. const array = [firsthtmlelement, secondhtmlelement];
  5. button.addEventListener("click", function() {
  6. array[1].innerHTML = "Hello World";
  7. });
  1. <div id="firsthtmlelement">div1</div>
  2. <div id="secondhtmlelement">div2</div>
  3. <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 -->

  1. let firsthtmlelement = document.getElementById(&quot;firsthtmlelement&quot;);
  2. let secondhtmlelement = document.getElementById(&quot;secondhtmlelement&quot;);
  3. const button = document.getElementById(&quot;button&quot;);
  4. const array = [firsthtmlelement, secondhtmlelement];
  5. button.addEventListener(&quot;click&quot;, function() {
  6. array[1].innerHTML = &quot;Hello World&quot;;
  7. });

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

  1. &lt;div id=&quot;firsthtmlelement&quot;&gt;div1&lt;/div&gt;
  2. &lt;div id=&quot;secondhtmlelement&quot;&gt;div2&lt;/div&gt;
  3. &lt;button id=&quot;button&quot;&gt;press&lt;/button&gt;

<!-- end snippet -->

答案2

得分: 0

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

  1. window["variableName"].innerHTML = myValue;
  1. let firsthtmlelement = document.getElementById("firsthtmlelement");
  2. let secondhtmlelement = document.getElementById("secondhtmlelement");
  3. const button = document.getElementById("button");
  4. const array = ['firsthtmlelement', 'secondhtmlelement'];
  5. button.addEventListener("click", function() {
  6. console.log(array[1]);
  7. // 在这个示例中,我们将硬编码要更改的元素
  8. window[array[1]].innerHTML = "Hello World";
  9. });
  1. <div id="firsthtmlelement">div1</div>
  2. <div id="secondhtmlelement">div2</div>
  3. <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 -->

  1. let firsthtmlelement = document.getElementById(&quot;firsthtmlelement&quot;);
  2. let secondhtmlelement = document.getElementById(&quot;secondhtmlelement&quot;);
  3. const button = document.getElementById(&quot;button&quot;);
  4. const array = [&#39;firsthtmlelement&#39;, &#39;secondhtmlelement&#39;];
  5. button.addEventListener(&quot;click&quot;, function() {
  6. console.log(array[1]);
  7. //in this example we will just hardcode which element to change
  8. window[array[1]].innerHTML = &quot;Hello World&quot;;
  9. });

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

  1. &lt;div id=&quot;firsthtmlelement&quot;&gt;div1&lt;/div&gt;
  2. &lt;div id=&quot;secondhtmlelement&quot;&gt;div2&lt;/div&gt;
  3. &lt;button id=&quot;button&quot;&gt;press&lt;/button&gt;

<!-- end snippet -->

答案3

得分: 0

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

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

下面我有两种解决方案:

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

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

  1. const elementsObject = {
  2. "firsthtmlelement": document.querySelector("#firsthtmlelement"),
  3. "secondhtmlelement": document.querySelector("#secondhtmlelement")
  4. };
  5. elementsObject["firsthtmlelement"].innerHTML = "Hello World 1";
  6. const elementsArray = [document.querySelector("#firsthtmlelement"), document.querySelector("#secondhtmlelement")];
  7. elementsArray[1].innerHTML = "Hello World 2";
  1. <div id="firsthtmlelement"></div>
  2. <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 -->

  1. const elementsObject = {
  2. &quot;firsthtmlelement&quot;: document.querySelector(&quot;#firsthtmlelement&quot;),
  3. &quot;secondhtmlelement&quot;: document.querySelector(&quot;#secondhtmlelement&quot;)
  4. };
  5. elementsObject[&quot;firsthtmlelement&quot;].innerHTML = &quot;Hello World 1&quot;;
  6. const elementsArray = [document.querySelector(&quot;#firsthtmlelement&quot;), document.querySelector(&quot;#secondhtmlelement&quot;)];
  7. elementsArray[1].innerHTML = &quot;Hello World 2&quot;;

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

  1. &lt;div id=&quot;firsthtmlelement&quot;&gt;&lt;/div&gt;
  2. &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:

确定