首页 > 解决方案 > 为什么 JS 每个函数只运行一次 document.Tastierino.Testo.value?

问题描述

我是 Javascript 的初学者,我想编写一个迷你游戏,我编写了所有代码并且它可以工作,除了代码似乎跳过第 56 行的小点,问题不在于有问题的行,因为如果我删除下一个两条线它工作。你能告诉我错误在哪里吗?

<!doctype html>
<html>
<head> 
<meta charset="UTF-8"> 
</head>
<body style="background-color: #4F4F4F">
<p>Guess the number
<center>
<form name="Tastierino">
<table>
<tr><td colspan="3"><textarea cols="17" rows="2" value="" name="Testo" readonly></textarea></td></tr>
<tr>
<td><center><input type="button" value="   7   " onClick="aggiungi('7')"></center></td>
<td><center><input type="button" value="   8   " onClick="aggiungi('8')"></center></td>
<td><center><input type="button" value="   9   " onClick="aggiungi('9')"></center></td>
</tr>
<tr>
<td><center><input type="button" value="   4   " onClick="aggiungi('4')"></center></td> 
<td><center><input type="button" value="   5   " onClick="aggiungi('5')"></center></td> 
<td><center><input type="button" value="   6   " onClick="aggiungi('6')"></center></td>
</tr>
<tr>  
<td><center><input type="button" value="   1   " onClick="aggiungi('1')"></center></td>
<td><center><input type="button" value="   2   " onClick="aggiungi('2')"></center></td> 
<td><center><input type="button" value="   3   " onClick="aggiungi('3')"></center></td>
</tr>
<tr> 
<td></td>
<td><center><input type="button" value="   0   " onClick="aggiungi('0')"></center></td>
<td></td>
</tr>
<tr><td colspan="3"><center><input type="button" value="    Conferma    " onClick="conferma()"></center></td></tr>
</table></form>
<script language="Javascript">
var Nrand=(Math.floor((Math.random() * 30) + 1));
var errori=0;

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    };
  };
};

function aggiungi(n) {
document.Tastierino.Testo.value=document.Tastierino.Testo.value+n;
};
function conferma(){
var Nscelto=document.Tastierino.Testo.value;
if(Nscelto==Nrand){
document.Tastierino.Testo.value="bravo hai vinto!";
}else{
if(Nscelto<Nrand){
document.Tastierino.Testo.value="è sbagliato il numero è maggiore";
sleep(2000);
document.Tastierino.Testo.value="";
}else{
document.Tastierino.Testo.value="è sbagliato il numero è minore";
sleep(2000);
document.Tastierino.Testo.value="";
}}};

</script>
</center>
</p>
</body>
</html>

标签: javascripthtml

解决方案


推荐阅读