我的分数增量不起作用。两个问题。
它会自动更新吗?
或者我的代码错了吗?
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++;
}
这么简单的代码,但它不起作用,显然我写错了但是我看不出有什么问题?
答案 0 :(得分:4)
你是document.write'得分,但从不更新通话,所以即使你的分数递增,它也没有在页面上更新......
使用以下内容更新您的HTML:
<!-- <script>document.write("Score: " + score);</script> -->
<span id="score"></span>
和你的javascript类似
function scoreIncrement() {
score++;
document.getElementById("score").innerText="Score: " + score;
}
应该这样做。最优雅的解决方案,也许不是,但确实有效。