将 javascript switch 语句打印到 html 页

print javascript switch statement to html page

抱歉,如果我解释错了,我是 javascript 的新手。我正在尝试将此 switch 语句打印到我的 html 页面,但我不确定该怎么做。任何帮助将不胜感激。

在我的 HTML 页面上我有:

<button type="button" onclick="weatherAssessment()">Weather Assassment</button>
<p id="result"> <p> 

我只是不确定如何使用 getElementbyID 将结果打印到页面。

JavaScript:

function weatherAssessment(){

var currentDate = new Date();
var currentMonth = currentDate.getMonth();

switch(currentMonth) {

    case 11:
    case 0:
    case 1:
        analysis = "It is chilly winter.";
        break;

    case 2:
    case 3:
    case 4:
        analysis = "A beautiful spring.";
        break;

    case 5:
    case 6:
    case 7:
        analysis = "The heat of summer.";
        break;

    case 8:
    case 9:
    case 10:
        analysis = "Fabulous Fall";
        break;

}

console.log(currentMonth);
console.log(analysis);

}

只需使用:

document.getElementById("result").innerHTML = "yourText";

试试这个...

<html>
<head>

</head>
<body>

  <button type="button" onclick="weatherAssessment()">Weather Assassment</button>
  <p id="result"> <p> 


</body>
</html>

<script>

  function weatherAssessment(){

    var currentDate = new Date();
    var currentMonth = currentDate.getMonth();

    switch(currentMonth) {

        case 11:
        case 0:
        case 1:
            analysis = "It is chilly winter.";
            break;

        case 2:
        case 3:
        case 4:
            analysis = "A beautiful spring.";
            break;

        case 5:
        case 6:
        case 7:
            analysis = "The heat of summer.";
            break;

        case 8:
        case 9:
        case 10:
            analysis = "Fabulous Fall";
            break;

    }

    // console.log(currentMonth);
    // console.log(analysis);

    document.getElementById("result").innerHTML = "Current Month: " + currentMonth + " Analysis: " + analysis;

  }

</script>