2015-01-13 40 views
2

当用户点击按钮的日子里,该脚本会生成一个随机数,然后显示相应的工作日。下面的代码是我到目前为止,唉,它似乎并没有工作。 我调试过它,显然myfunction没有定义?!使用功能,显示随机数,然后ifelse语句来分配一周

<!DOCTYPE html> 
    <html> 

    <body> 

    <p>Click the button to display a random number between 1 and 7</p> 

    <button onclick="myFunction()">Get Random</button> 

    <p id="task1"></p> 

    <script> 
     function myFunction() { 
     var x = Math.floor((Math.random() * 7 + 1); 
     document.getElementById("task1").innerHTML = x; 
     } 

     if (x===1) 
     { 
      document.write("today is sunday"); 
     } 
     else if(x===2) 
     { 
      document.write("today is monday"); 
     } 
     else if(x===3) 
     { 
      document.write("today is tuesday"); 
     } 
     else if(x===4) 
     { 
      document.write("today is wednesday"); 
     } 
     else if(x===5) 
     { 
      document.write("today is thursday"); 
     } 
     else if(x===6) 
     { 
      document.write("today is friday"); 
     } 
     else if(x===7) 
     { 
      document.write("today is saturday"); 
     } 

    </script> 

    </body> 

    </html> 
+0

我注意到var x = Math.floor((Math.random()* 7 + 1);中的括号是不平衡的。 – eigenchris

回答

0

var x是在myFunction体定义当地变量。它不能在您的功能之外访问(在您所有的if中)。

你也有你的var x = Math.floor(Math.random() * 7 + 1);声明多余的左括号。

修正版本:

function myFunction() { 
    var x = Math.floor(Math.random() * 7 + 1); 
    document.getElementById("task1").innerHTML = x;   

    if (x===1) 
    { 
     document.write("today is sunday"); 
    } 
    else if(x===2) 
    { 
     document.write("today is monday"); 
    } 
    else if(x===3) 
    { 
     document.write("today is tuesday"); 
    } 
    else if(x===4) 
    { 
     document.write("today is wednesday"); 
    } 
    else if(x===5) 
    { 
     document.write("today is thursday"); 
    } 
    else if(x===6) 
    { 
     document.write("today is friday"); 
    } 
    else if(x===7) 
    { 
     document.write("today is saturday"); 
    } 
} 
+0

谢谢,感谢您的解释和修复 – Rojito

+0

@Rojito欢迎您! – zavg

0

您有一个额外的括号和代码抛出异常的函数被渲染之前。

变种X = Math.floor(的Math.random()* 7 + 1); 的document.getElementById( “任务1”)的innerHTML = X。;

的,你可以使用浏览器控制台来查看到底是什么地方出了错。

1

你必须在Math.floor功能的额外括号。尝试运行以下内容,您会看到这样的作品。

function myFunction() { 
 
    var x = Math.floor(Math.random() * 7 + 1); 
 
    document.getElementById("task1").innerHTML = x; 
 
    
 
    if (x===1) 
 
    { 
 
     document.write("today is sunday"); 
 
    } 
 
    else if(x===2) 
 
    { 
 
     document.write("today is monday"); 
 
    } 
 
    else if(x===3) 
 
    { 
 
     document.write("today is tuesday"); 
 
    } 
 
    else if(x===4) 
 
    { 
 
     document.write("today is wednesday"); 
 
    } 
 
    else if(x===5) 
 
    { 
 
     document.write("today is thursday"); 
 
    } 
 
    else if(x===6) 
 
    { 
 
     document.write("today is friday"); 
 
    } 
 
    else if(x===7) 
 
    { 
 
     document.write("today is saturday"); 
 
    } 
 
}
<p>Click the button to display a random number between 1 and 7</p> 
 

 
    <button onclick="myFunction()">Get Random</button> 
 

 
    <p id="task1"></p>

0

,如果你使用它可以更容易和更短和对象例如

//declare the object that holds the days. 
 
    var days=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]; 
 

 
//declare your function that's triggered when clicking the button. 
 
    function myFunction(){ 
 

 
//declare your variable x that generates random numbers between 0 and 7 , 
 
// it starts from 0 because the first day "sunday" is days[0] not days[1]. 
 
    var x = Math.floor(Math.random()*7); 
 

 
//write the result in the p element 
 
    document.getElementById("task1").innerHTML ="today is "+ days[x]; 
 
    }
<button onclick="myFunction()">Click Here</button> 
 
<p id="task1"></p>

+0

用代码回答问题时,应该包含一些关于代码如何回答问题的文字说明。通常,仅有代码的答案对其他人不太有用。 –