使用javascript,出于某种原因我无法输出"星期二"即使d.getDay()为2,也可以通过该函数。
<!DOCTYPE html>
<html>
<body>
<p>The getDay() method returns the weekday as a number:</p>
<p id="demo"></p>
<script>
function getDayOfWeek(day){
if (day == 1){
return "Monday";
else if (day == 2){
return "Tuesday";
else{
return "Otherday";
}
}
var d = new Date();
document.getElementById("demo").innerHTML = getDayOfWeek(d.getDay());
</script>
</body>
</html>
答案 0 :(得分:4)
看起来你错过了else语句中的结束花括号。我已在下面更新了您的代码段。
function getDayOfWeek(day){
if (day == 1) {
return "Monday";
} else if (day == 2) {
return "Tuesday";
} else {
return "Otherday";
}
}
var d = new Date();
document.getElementById("demo").innerHTML = getDayOfWeek(d.getDay());
答案 1 :(得分:3)
检查大括号。工作版:
<!DOCTYPE html>
<html>
<body>
<p>The getDay() method returns the weekday as a number:</p>
<p id="demo"></p>
<script>
function getDayOfWeek(day){
if (day == 1)
{
return "Monday";
}
else if (day == 2)
{
return "Tuesday";
}
else
{
return "Otherday";
}
}
var d = new Date();
document.getElementById("demo").innerHTML = getDayOfWeek(d.getDay());
</script>
</body>
</html>
答案 2 :(得分:0)
要解决这个问题非常容易,你只需忘记关闭'if',然后'else if'使用this =&gt; '}'
<!DOCTYPE html>
<html>
<body>
<p>The getDay() method returns the weekday as a number:</p>
<p id="demo"></p>
<script>
function getDayOfWeek(day){
if (day == 1){
return "Monday";
}else if (day == 2){
return "Tuesday";
}else{
return "Otherday";
}
}
var d = new Date();
document.getElementById("demo").innerHTML = getDayOfWeek(d.getDay());
</script>
</body>