innerHTML 不更新 DIV

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

innerHTML not updating DIV

问题

我想要一个动态页面,根据URL参数显示特定玩家是真实玩家还是基于计算机的。参数本身工作正常,数据也正确收集,但我有的DIV标签没有更新它们的文本。

问题与声明变量p1到p6有关,它们在控制台中显示为null。我不确定为什么会这样,因为我甚至是在onload函数内调用整个内容。每个case结束时的console.log调用也正常工作。

代码片段

<div class="wrapper">
  <div></div>
  <div id="p1">p1</div>
  <div id="p2">p2</div>
  <div></div>
  <div id="p3">p3</div>
  <div></div>
  <div></div>
  <div id="p4">p4</div>
  <div></div>
  <div id="p5">p5</div>
  <div id="p6">p6</div>
  <div></div>
</div>
window.onload = function() {
  var p1 = document.getElementById("p1"),
    p2 = document.getElementById("p2"),
    p3 = document.getElementById("p3"),
    p4 = document.getElementById("p4"),
    p5 = document.getElementById("p5"),
    p6 = document.getElementById("p6");
  var url = window.location.href,
    params = url.split('?')[1].split('&'),
    data = {},
    tmp;
  for (var i = 0, l = params.length; i < l; i++) {
    tmp = params[i].split('=');
    data[tmp[0]] = tmp[1];
  }

  switch (data.players) {
    case "1":
      p2.innerHTML = "Computer";
      p3.innerHTML = "Computer";
      p4.innerHTML = "Computer";
      p5.innerHTML = "Computer";
      p6.innerHTML = "Computer";
      console.log(data.players)
      break;
    case "2":
      p3.innerHTML = "Computer";
      p4.innerHTML = "Computer";
      p5.innerHTML = "Computer";
      p6.innerHTML = "Computer";
      console.log(data.players)
      break;
    case "3":
      p4.innerHTML = "Computer";
      p5.innerHTML = "Computer";
      p6.innerHTML = "Computer";
      console.log(data.players)
      break;
    case "4":
      p5.innerHTML = "Computer";
      p6.innerHTML = "Computer";
      console.log(data.players)
      break;
    case "5":
      p6.innerHTML = "Computer";
      console.log(data.players)
      break;
    default:
      console.log(data.players)
      break;
  }
}

希望这对你有所帮助。

英文:

I want to have a dynamic page that displays whether a certain player is a real player or a computer based on url parameters. The parameters themselves work fine and the data is collected correctly, but the DIV tags I have aren't updating their text.

The issue is to do with declaring the variables p1 to p6, which are being shown as null in the console. I'm unsure on why this is since I'm even calling the whole thing inside the onload function. The console.log calls at the end of each case are also working fine.

Code Snippet

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

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

window.onload = function() {
var p1 = document.getElementById(&quot;p1&quot;),
p2 = document.getElementById(&quot;p2&quot;),
p3 = document.getElementById(&quot;p3&quot;),
p4 = document.getElementById(&quot;p4&quot;),
p5 = document.getElementById(&quot;p5&quot;),
p6 = document.getElementById(&quot;p6&quot;);
var url = window.location.href,
params = url.split(&#39;?&#39;)[1].split(&#39;&amp;&#39;),
data = {},
tmp;
for (var i = 0, l = params.length; i &lt; l; i++) {
tmp = params[i].split(&#39;=&#39;);
data[tmp[0]] = tmp[1];
}
switch (data.players) {
case 1:
p2.innerHTML = &quot;Computer&quot;;
p3.innerHTML = &quot;Computer&quot;;
p4.innerHTML = &quot;Computer&quot;;
p5.innerHTML = &quot;Computer&quot;;
p6.innerHTML = &quot;Computer&quot;;
console.log(data.players)
break;
case 2:
p3.innerHTML = &quot;Computer&quot;;
p4.innerHTML = &quot;Computer&quot;;
p5.innerHTML = &quot;Computer&quot;;
p6.innerHTML = &quot;Computer&quot;;
console.log(data.players)
break;
case 3:
p4.innerHTML = &quot;Computer&quot;;
p5.innerHTML = &quot;Computer&quot;;
p6.innerHTML = &quot;Computer&quot;;
console.log(data.players)
break;
case 4:
p5.innerHTML = &quot;Computer&quot;;
p6.innerHTML = &quot;Computer&quot;;
console.log(data.players)
break;
case 5:
p6.innerHTML = &quot;Computer&quot;;
console.log(data.players)
break;
default:
console.log(data.players)
break;
}
}

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

&lt;div class=&quot;wrapper&quot;&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div id=&quot;p1&quot;&gt;p1&lt;/div&gt;
&lt;div id=&quot;p2&quot;&gt;p2&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div id=&quot;p3&quot;&gt;p3&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div id=&quot;p4&quot;&gt;p4&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div id=&quot;p5&quot;&gt;p5&lt;/div&gt;
&lt;div id=&quot;p6&quot;&gt;p6&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

我清理了你的代码,同时保持了我的答案易懂。

首先,你将想要使用 DOMContentLoaded 而不是 window.onload,因为 DOMContentLoaded 会在HTML加载/解析后触发。

接下来,摆脱id是最好的长期选择。它们可能难以处理并且有更多的限制。

我使用URL的 searchParams 来解析查询字符串,而不是拆分它。

使用 Number,我将从查询字符串返回的字符串转换为数字。

我有一些双重检查功能来验证传递的变量是否在正确的范围内。

然后,我简单地循环遍历从起始玩家编号到玩家div的长度的范围。

addEventListener("DOMContentLoaded", (event) => {
  
  //const url = new URL(window.location.href);
  const url = new URL("https://example.com/?players=4");
  let players = Number(url.searchParams.get("players"));
  
  const playerDivs = document.querySelectorAll("[data-player]");
  
  if(players > playerDivs.length) players = playerDivs.length;
  else if(players <= 0) players = 1;
  
  for(let c = players; c < playerDivs.length; c++) {
    playerDivs[c].innerHTML = "COMPUTER";
  }
  
});
<div class="wrapper">
  <div></div>
  <div data-player="1">p1</div>
  <div data-player="2">p2</div>
  <div></div>
  <div data-player="3">p3</div>
  <div></div>
  <div></div>
  <div data-player="4">p4</div>
  <div></div>
  <div data-player="5">p5</div>
  <div data-player="6">p6</div>
  <div></div>
</div>
英文:

I cleaned up your code while keeping my answer understandable.

First, you will want to use DOMContentLoaded instead of window.onload as DOMContentLoaded will be triggered after the HTML is loaded/parsed.

Next, getting rid of ids is the best long term option. They can be difficult to work with and have more restrictions.

I'm using URL searchParams to parse the query string instead of splitting it.

Using Number I convert the string to a number that is returned from the query string.

I have some double checking functionality to verify that the variable passed is in the proper range.

Then I simply loop through the the starting player number and go to the end of the length of the player divs.

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

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

addEventListener(&quot;DOMContentLoaded&quot;, (event) =&gt; {
//const url = new URL(window.location.href);
const url = new URL(&quot;https://example.com/?players=4&quot;);
let players = Number(url.searchParams.get(&quot;players&quot;));
const playerDivs = document.querySelectorAll(&quot;[data-player]&quot;);
if(players &gt; playerDivs.length)players = playerDivs.length
else if(players &lt;= 0)players = 1;
for(let c = players;c&lt;playerDivs.length;c++){
playerDivs[c].innerHTML = &quot;COMPUTER&quot;;
}
});

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

&lt;div class=&quot;wrapper&quot;&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div data-player=&quot;1&quot;&gt;p1&lt;/div&gt;
&lt;div data-player=&quot;2&quot;&gt;p2&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div data-player=&quot;3&quot;&gt;p3&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div data-player=&quot;4&quot;&gt;p4&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;div data-player=&quot;5&quot;&gt;p5&lt;/div&gt;
&lt;div data-player=&quot;6&quot;&gt;p6&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

你主要问题是查询参数的类型是字符串,但你使用了 case 1: 而不是 case "1":。除此之外,我不建议使用 switch case

不要使用ID,而是使用类,如:

<div class="player">p1</div>
<div class="player">p2</div>
<div class="player">p3</div>
<div class="player">p4</div>
<div class="player">p5</div>
<div class="player">p6</div>

你所需要的所有JavaScript代码如下:

const urlParams = new URLSearchParams(window.location.search);
const elsPlayer = document.querySelectorAll(".player");
const totComputer = elsPlayer.length - urlParams.get("players");
[...elsPlayer].slice(-totComputer).forEach(el => {
  el.textContent = "Computer";
});
  • 使用 querySelectorAll 获取你的 .player DIVs 的NodeList。

  • 使用 new URLSearchParams(window.location.search).get() 方法从URI查询参数中检索人类玩家数量的字符串。

  • 使用 .slice(-N) 获取最后 N 个DIV元素。

  • 使用 .forEach() 遍历你的(切片的,最后 N 个)元素并更改它们的 textContent

英文:

Your main issue is that a query param is of type String, but you're using case 1: instead of case &quot;1&quot;:. Besides that I would not use switch case at all.

Instead of using IDs, use classes like

&lt;div class=&quot;player&quot;&gt;p1&lt;/div&gt;
&lt;div class=&quot;player&quot;&gt;p2&lt;/div&gt;
&lt;div class=&quot;player&quot;&gt;p3&lt;/div&gt;
&lt;div class=&quot;player&quot;&gt;p4&lt;/div&gt;
&lt;div class=&quot;player&quot;&gt;p5&lt;/div&gt;
&lt;div class=&quot;player&quot;&gt;p6&lt;/div&gt;

All the JavaScript you need is:

const urlParams = new URLSearchParams(window.location.search);
const elsPlayer = document.querySelectorAll(&quot;.player&quot;);
const totComputer = elsPlayer.length - urlParams.get(&quot;players&quot;);
[...elsPlayer].slice(-totComputer).forEach(el =&gt; {
  el.textContent = &quot;Computer&quot;;
});
  • Use querySelectorAll to get a NodeList of your .player DIVs
  • Use new URLSearchParams(window.location.search) and the .get() method to retrieve the String of the number of human players from the URI query search param
  • Use .slice(-N) to get the last N DIV elements
  • Use .forEach() to iterate your (sliced, last N) elements and change their textContent

huangapple
  • 本文由 发表于 2023年7月7日 05:45:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76632693.html
匿名

发表评论

匿名网友

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

确定