gpt4 book ai didi

javascript - 选择表格中的正确单元格时显示正确答案

转载 作者:搜寻专家 更新时间:2023-10-31 08:47:42 25 4
gpt4 key购买 nike

我在下面写了一段代码。 What I am trying to do here is, when the three right cells are selected, it should show a message saying "Right", if not then should say "Try again".到目前为止,我的代码只突出显示单元格。我想知道下一步该怎么做?

   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style>
table {
width:400px;
font-family:Verdana, Arial, Helvetica, sans-serif;
border:1px solid #000;
}
table td {border:1px solid #000;}
.bgrd {background-color:#CCCCCC;cursor:pointer;}
.bgrd_selected {background-color:#FFCC00;cursor:pointer;}

</style>
<script type="text/javascript">
function setColor(what)
{
var thetds = document.getElementsByTagName('td');
for(var i = 0; i < thetds.length; i++)
{
if(thetds[i].className == 'bgrd_selected')
{
thetds[i].className = 'bgrd';
}
}
what.className = 'bgrd_selected';
}
</script>
</head>
<body>
<table cellspacing="0" cellpadding="0">
<tr>
<td class="bgrd" onclick="setColor(this)">
Select</td>
<td class="bgrd" onclick="setColor(this)">
Select</td>
<td class="bgrd" onclick="setColor(this)">
Select</td>

</tr>
<tr>
<td class="bgrd" onclick="setColor(this)">
Select</td>
<td class="bgrd" onclick="setColor(this)">
Select</td>
<td class="bgrd" onclick="setColor(this)">
Select</td>

</tr>


<tr>
<td class="bgrd" onclick="setColor(this)">
Select</td>
<td class="bgrd" onclick="setColor(this)">
Select</td>
<td class="bgrd" onclick="setColor(this)">
Select</td>

</tr>

<tr>
<td class="bgrd" onclick="setColor(this)">
Select</td>
<td class="bgrd" onclick="setColor(this)">
Select</td>
<td class="bgrd" onclick="setColor(this)">
Select</td>

</tr>

<tr>
<td class="bgrd" onclick="setColor(this)">
Select</td>
<td class="bgrd" onclick="setColor(this)">
Select</td>
<td class="bgrd" onclick="setColor(this)">
Select</td>

</tr>

<tr>
<td class="bgrd" onclick="setColor(this)">
Select</td>
<td class="bgrd" onclick="setColor(this)">
Select</td>
<td class="bgrd" onclick="setColor(this)">
Select</td>

</tr>

<tr>
<td class="bgrd" onclick="setColor(this)">
Select</td>
<td class="bgrd" onclick="setColor(this)">
Select</td>
<td class="bgrd" onclick="setColor(this)">
Select</td>

</tr>
</table>
</body>
</html>

谢谢

最佳答案

如果有特定的正确答案,您需要唯一地识别每种可能性。我使用了 id 标签

HTML

<table cellspacing="0" cellpadding="0">
<tr>
<td class="bgrd" id="A1" onclick="setColor(this)">Select</td>
<td class="bgrd" id="A2" onclick="setColor(this)">Select</td>
<td class="bgrd" id="A3" onclick="setColor(this)">Select</td>
</tr>
<tr>
<td class="bgrd" id="B1" onclick="setColor(this)">Select</td>
<td class="bgrd" id="B2" onclick="setColor(this)">Select</td>
<td class="bgrd" id="B3" onclick="setColor(this)">Select</td>
</tr>
[ ... ]

接下来,Javascript。这需要定义您的正确答案,切换用户单击的元素的类名,然后计算所选元素的数量和所选正确答案的数量。最后,如果选择的数字与正确答案的数量相匹配,他们就是正确的!

var answers = ["A1","B2","C3"];

function setColor(what){
// Toggle the class name for the selected element
what.className = (what.className == 'bgrd' ? 'bgrd_selected' : 'bgrd');

// Set number of correct answers and selected answers to 0
var numCorrect = 0,
numSelected = 0;

// Loop all relevant elements
var thetds = document.getElementsByTagName('td');
for(var i = 0; i < thetds.length; i++){
// if an element is selected, add one to numSelected
if(thetds[i].className == 'bgrd_selected'){
numSelected++;
}
}

// Loop all possible correct answers
for(var i = 0; i < answers.length; i++){
// If an answer is selected, add one to numCorrect
if(document.getElementById(answers[i]).className == 'bgrd_selected'){
numCorrect++;
}
}
// Determine if there are the same number of numCorrect and numSelected as there are answers
document.getElementById("out").innerHTML =
(numCorrect == answers.length && numSelected == answers.length ? "Right" : "Try Again!");
}

http://jsfiddle.net/aWsCu/

当然,有几种方法可以做到这一点,这绝不是最好的或最干净的……但是,这是一个开始。

关于javascript - 选择表格中的正确单元格时显示正确答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13578043/

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