为什么这个简单的代码会产生错误?

时间:2018-07-09 13:11:58

标签: javascript

var d = new Date();
var day;
switch (d.getDay()) {
    case 0 :
      day = "Sunday";
      break;
    case 1 :
      day = "Monday";
      break;
    case 2 :
      day = "Tuesday";
      break;
    case 3 :
      day = "Wednesday";
      break;
    case 4 :
      day = "Thursday";
      break;
    case 5 :
      day = "Friday";
      break;
    default :
      day = "Saturday";
  }

 document.getElementById("demo").innerHTML = "Today is : " ;

1 个答案:

答案 0 :(得分:-1)

添加HTML和缺少的day,并在赋值前末尾添加

var d = new Date();
var day;
switch (d.getDay()) {
  case 0:
    day = "Sunday";
    break;
  case 1:
    day = "Monday";
    break;
  case 2:
    day = "Tuesday";
    break;
  case 3:
    day = "Wednesday";
    break;
  case 4:
    day = "Thursday";
    break;
  case 5:
    day = "Friday";
    break;
  default:
    day = "Saturday";
}

document.getElementById("demo").innerHTML = "Today is : " + day;
<div id="demo"></div>