gpt4 book ai didi

javascript - 如何将 span 元素的内容更改为变量的内容

转载 作者:行者123 更新时间:2023-11-28 17:09:28 26 4
gpt4 key购买 nike

我正在做一个石头剪刀布游戏,大部分编码都已完成,除了我没有向按钮添加功能,无论您点击什么,您都会选择布。

在添加该功能之前,我希望在网页上更新分数,但每当它尝试更新时,我都会得到:

Uncaught TypeError: Cannot set property 'textContent' of null

let choices = ["rock", "paper", "scissors"]
let localPlay = "paper"
let cpuChoice;
let scorePlayer = 0
let scoreCPU = 0
let playerPoints = document.getElementById("pScore")
let cpuPoints = document.getElementById("cScore")

function cpuPlay() {
cpuChoice = choices[Math.floor(Math.random() * choices.length)];
return cpuChoice
}

function gameConditions() {
// DRAW
if (localPlay === cpuChoice) {
console.log("draw")

// ROCK
} else if (localPlay === choices[0] && cpuChoice === choices[1]) {
scoreCPU = scoreCPU + 1
return cpuPoints.textContent = scoreCPU;
//cpu
} else if (localPlay === choices[0] && cpuChoice === choices[2]) {
scorePlayer = scorePlayer + 1
return playerPoints.textContent = scorePlayer;
//player

// PAPER
} else if (localPlay === choices[1] && cpuChoice === choices[0]) {
scorePlayer = scorePlayer + 1
return playerPoints.textContent = scorePlayer;
//player
} else if (localPlay === choices[1] && cpuChoice === choices[2]) {
scoreCPU = scoreCPU + 1
return cpuPoints.textContent = scoreCPU;
//cpu

// SCISSORS
} else if (localPlay === choices[2] && cpuChoice === choices[0]) {
scoreCPU = scoreCPU + 1
return cpuPoints.textContent = scoreCPU;
//cpu
} else if (localPlay === choices[2] && cpuChoice === choices[1]) {
scorePlayer = scorePlayer + 1
return playerPoints.textContent = scorePlayer;
//player
// DEBUG
} else {
console.log("not working")
}
}

function shoot() {
cpuPlay();
gameConditions();
console.log(cpuChoice)
imageChanger();
console.log("Player score: " + scorePlayer)
console.log("Cpu score: " + scoreCPU)
}

function imageChanger() {
let cpuImage = document.getElementById("cpuImg")
if (cpuChoice === choices[0]) {
cpuImage.src = "https://rodpa715.github.io/rock-paper-scissors/images/rock.png"
} else if (cpuChoice === choices[1]) {
cpuImage.src = "https://rodpa715.github.io/rock-paper-scissors/images/paper.png"
} else if (cpuChoice === choices[2]) {
cpuImage.src = "https://rodpa715.github.io/rock-paper-scissors/images/scissors.png"
}
}
* {
background-color: #9f85db;
box-sizing: border-box;
font-family: Arial, Helvetica, sans-serif;
}

h1 {
font-size: 24px;
}

h2 {
font-size: 18px;
}

.choices {
display: inline-block;
height: 175px;
width: 175px;
border: 2px solid black;
border-radius: 5px;
padding: 10px;
margin: 10px;
text-align: center;
cursor: pointer;
}

.choices img {
height: 150px;
width: 150px;
}
<!DOCTYPE html>
<html lang="en">

<head>
<title>Rock Paper Scissors</title>
<script type="text/javascript" src="./game.js"></script>
<!--<link href="./style.css" type="text/css" rel="stylesheet">-->
</head>

<body>
<h1>Rock Paper Scissors</h1>

<div class="score">
<h3>Player score: <span id="pScore"> 0 </span></h3>
<h3>Cpu score: <span id="cScore"> 0 </span> </h3>
</div>

<div class="cpu">
<h2>Cpu has chosen :</h2>
<div class="choices"><img id="cpuImg" src=""></div>
</div>

<div class="player">
<h2>Choose one :</h2>

<div class="choices">
<img src="https://rodpa715.github.io/rock-paper-scissors/images/rock.png" onclick="shoot();">
</div>

<div class="choices">
<img src="https://rodpa715.github.io/rock-paper-scissors/images/paper.png" onclick="shoot();">
</div>

<div class="choices">
<img src="https://rodpa715.github.io/rock-paper-scissors/images/scissors.png" onclick="shoot();">
</div>
</div>

</body>

</html>

这是我的 JsFiddle这是我的Github Repo这是 GitHub Page

这与我尝试使用变量设置 .textcontent 有关吗?

最佳答案

这是 JS 和 HTML 中最常见的问题。 JS 将在整个 HTML 之前加载,因此当您尝试创建 DOM 变量时,什么也没有(跨度尚未加载)。因此,您可以创建一个 onLoad 事件,该事件将保存这些变量,或者不创建这些变量,并将值直接设置为 span。

关于javascript - 如何将 span 元素的内容更改为变量的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54852943/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com