打印javascript开关语句到HTML页面

print javascript switch statement to html page

本文关键字:HTML 页面 语句 javascript 开关 打印      更新时间:2023-09-26

对不起,如果我解释这个错误,我是非常新的javascript。我试图打印这个开关语句到我的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);
}

试试这个…

<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>

直接使用:

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