<!DOCTYPE html>
<html>
<head>
<style type="text/css">
div.dice {
float:left;
width:32px;
background:#F5F5F5;
border:#999 1px solid;
padding:10px;
font-size:24px;
text-align:center;
margin:5px;
}
</style>
<script>
function rollDice() {
var die1 = document.getElementById("die1");
var die2 = document.getElementById("die2");
var status = document.getElementById("status");
var d1 = Math.floor(Math.random() * 6) + 1;
var d2 = Math.floor(Math.random() * 6) + 1;
var wins = documentgetElementById("doubles");
var diceTotal = d1 + d2;
die1.innerHTML = d1;
die2.innerHTML = d2;
status.innerHTML = "You rolled " + diceTotal + ".";
if (d1 == d2) {
status.innerHTML += " DOUBLES! You get a free turn!!";
}
}
</script>
</head>
<body>
<div id="die1" class="dice">0</div>
<div id="die2" class="dice">0</div>
<button onclick="rollDice()">Roll Dice</button>
<h2 id="status" style="clear:left;"></h2>
<br>Wins:
<h5 id="doubles" style="clear:left;"></h5>
<br>
</body>
</html>
我想拥有它,所以Wins每次滚动双打时都会更新
我仍然在努力,我认为这将是一个简单的代码行后添加
status.innerHTML += " DOUBLES! You get a free turn!!";
doubles.innerHTML =+ Wins;
put var wins = document.getElementById("doubles");
???我知道我做错了什么,任何帮助都会很棒!真诚的LM
答案 0 :(得分:2)
var wins = documentgetElementById("doubles");
有拼写错误,应为var wins = document.getElementById("doubles");
至于更新win计数器,你需要在添加1之前将元素的值转换为int。但是,我建议将win计数存储在全局变量中,而不是存储在DOM元素中的文本中。
if (d1 == d2) {
status.innerHTML += " DOUBLES! You get a free turn!!";
var winsCount = parseInt(wins.innerHTML) || 0;
wins.innerHTML = winsCount + 1;
}
答案 1 :(得分:1)
我看到了你的问题。您声明变量wins
的行上有拼写错误,documentgetElementById()
应为document.getElementById()
。在胜利h5中以0开始,然后每次玩家获胜时都执行此代码:
var previouswins = document.getElementById("doubles").innerHTML;
var presentwins = parseInt(previouswins) + 1;
document.getElementById("doubles").innerHTML = presentwins;
该代码创建了一个名为previouswins
的变量,它基本上就是doubles
h5
内的所有内容。然后我们将其解析为整数,这意味着javascript现在可以用它做数学函数,然后我们添加一个。之后剩下要做的就是将新变量写入doubles
h5
。
希望有所帮助:)