gpt4 book ai didi

javascript - 分数不递增javascript

转载 作者:行者123 更新时间:2023-11-30 10:43:03 24 4
gpt4 key购买 nike

我的分数增量不起作用。两个问题。

会自动更新吗?

还是我的代码有误?

HTML 页面

<script>document.write("Score: " + score);</script>

<table class="widthOneHundredPercent">
<tr>
<td class="tableCellFloat columnOne" valign="top">
<div class="question"><b>Question 1:</b> Who is the youngest in B Media?</div>
<td class="tableCellFloat columnTwo" valign="top">
<div class="answers">
<ol>
<li>
<input type="radio" name="q1" id="q1-a" onclick="unhide(this); scoreIncrement()"/>John
</li>
<li>
<input type="radio" name="q1" id="q1-b" onclick="unhide(this)"/>Paula
</li>
<li>
<input type="radio" name="q1" id="q1-c" onclick="unhide(this)"/>Henal
</li>
<li>
<input type="radio" name="q1" id="q1-d" onclick="unhide(this)"/>Malc
</li>
</ol>
</div>
</td>
</td>
</tr>
<tr>
<td colspan="2" valign="top">
<div id="answerq1a" class="hidden">
<p>Your answer is correct, John is 20.</p>
</div>
</td>
</tr>
<tr>
<td colspan="2" valign="top">
<div id="answerq1b" class="hidden">
<p>Your answer is incorrect, Paula is 31.</p>
</div>
</td>
</tr>
<tr>
<td colspan="2" valign="top">
<div id="answerq1c" class="hidden">
<p>Your answer is incorrect, Henal is 21.</p>
</div>
</td>
</tr>
<tr>
<td colspan="2" valign="top">
<div id="answerq1d" class="hidden">
<p>Your answer is incorrect, Malc is 33.</p>
</div>
</td>
</tr>
</table>​

Javascript

var currentShown = "";

function unhide(rad) {
var id = "answer" + rad.id.replace("-", "");
var answer = document.getElementById(id);
if (answer) {
var current = document.getElementById(currentShown);
if (current) current.className = "hidden";
currentShown = id;
answer.className = "unhidden";
}
}

var score = 0;

function scoreIncrement() {

score++;
}​

这么简单的代码,居然不行,明明写错了却看不出哪里不对?

最佳答案

你正在记录。写出分数,但从不更新调用,所以即使你的分数增加了,它也没有在页面上更新......

用类似的东西更新你的 HTML:

<!-- <script>document.write("Score: " + score);</script> -->
<span id="score"></span>

和你的 javascript 之类的东西

function scoreIncrement() {
score++;
document.getElementById("score").innerText="Score: " + score;
}​

应该可以了。最优雅的解决方案,也许不是,但它确实有效。

关于javascript - 分数不递增javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9979963/

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