gpt4 book ai didi

javascript - 改变 innerHTML 只工作一次

转载 作者:太空宇宙 更新时间:2023-11-04 13:35:53 25 4
gpt4 key购买 nike

我正在尝试用 HTML 制作记分牌,其中的 javascript 函数可以更改不同玩家的分数。但是,除一名玩家外,所有玩家的分数都无法正确显示。

HTML:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<div id="scoreboard"></div>
<script type="text/javascript" src="main.js"></script>
</body>
</html>

Javascript:

function Player(ID) {
this.ID = ID;

var scoreboard = document.getElementById("scoreboard");

//Create a <p> in scoreboard with "Player n:" in it, and then
//create a <span> with the score and a unique ID to use for getting and setting
scoreboard.innerHTML += "<p>Player " + this.ID + ":" + "<span id=\"PlayerScore" + this.ID + "\">0</span></p>";

this.scoreElement = document.getElementById("PlayerScore" + this.ID); //set score element

//Getter and setter for score
this.getScore = function() {return Number(this.scoreElement.innerHTML);}
this.setScore = function(scoreIn) {this.scoreElement.innerHTML = scoreIn.toString();}
}

我使用以下 Javascript 测试此代码:

var player1 = new Player(1);
var player2 = new Player(2);
var player3 = new Player(3);

player1.setScore(player1.getScore() + 1);
player2.setScore(player2.getScore() + 1);
player3.setScore(player3.getScore() + 1);

执行此操作后,生成的网页中的记分牌显示为:

Player 1:0
Player 2:0
Player 3:1

但是,它应该是

Player 1:1
Player 2:1
Player 3:1

有什么想法吗?为什么只有最后一个有效?我如何直接从 scoreElement 的 InnerHTML 获取和设置有问题吗?和我的浏览器(firefox)有关系吗?

最佳答案

问题是您使用 innerHTML 附加更多内容,一旦您使用 innerHTML += 它将重新创建所有 dom 元素,因此您的 scoreElement 您之前创建的引用将不再出现在 dom 树中,这就是那些没有得到更新的原因。

解决方法不是使用.innerHTML追加内容,而是创建dom元素,调用appendChild()方法将其追加到父元素中

function Player(ID) {
this.ID = ID;

var scoreboard = document.getElementById("scoreboard");

var playerElement = document.createElement('p');
this.scoreElement = document.createElement('span');
this.scoreElement.innerHTML = 0;
playerElement.appendChild(document.createTextNode("Player " + this.ID + ":"));
playerElement.appendChild(this.scoreElement);
scoreboard.appendChild(playerElement)

//Getter and setter for score
this.getScore = function() {
return Number(this.scoreElement.innerHTML);
}
this.setScore = function(scoreIn) {
this.scoreElement.innerHTML = scoreIn.toString();
}
}

var player1 = new Player(1);
var player2 = new Player(2);
var player3 = new Player(3);

player1.setScore(player1.getScore() + 1);
player2.setScore(player2.getScore() + 1);
player3.setScore(player3.getScore() + 1);
<div id="scoreboard"></div>


作为@dandaviscomments 中说,您可以做的另一个调整是

function Player(ID) {
this.ID = ID;

var scoreboard = document.getElementById("scoreboard");

var playerElement = document.createElement('p');
playerElement.innerHTML = "Player " + this.ID + ":" + '<span>0</span>';
this.scoreElement = playerElement.querySelector('span');
scoreboard.appendChild(playerElement)

//Getter and setter for score
this.getScore = function() {
return Number(this.scoreElement.innerHTML);
}
this.setScore = function(scoreIn) {
this.scoreElement.innerHTML = scoreIn.toString();
}
}

var player1 = new Player(1);
var player2 = new Player(2);
var player3 = new Player(3);

player1.setScore(player1.getScore() + 1);
player2.setScore(player2.getScore() + 1);
player3.setScore(player3.getScore() + 1);
<div id="scoreboard"></div>

关于javascript - 改变 innerHTML 只工作一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33210045/

25 4 0
文章推荐: java - 在 Java 7/Guava 中过滤集合的更干净的方法?
文章推荐: jquery - 如何滚动绝对div?
文章推荐: java - Controller /Servlet 在 ServiceAPI 上抛出 NoClassDefFoundError
文章推荐: html - Angularjs
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com