无法弄清楚为什么我的标题的其余部分不会显示

Can't figure out why the rest of my header will not show

本文关键字:余部 显示 弄清楚 为什么 我的 标题      更新时间:2023-09-26
    <head>

页面加载时调用的函数

    <body onload="myOnload()">

带有自定义图形的标题 div

    <div id="header">
    <img src="resumeheader.png" alt="Header" style="width:750px;height:100px;">
    <h2>Pay Calculator</h2>
    <h3></h3>
    <script>

此功能显示小时

    function myOnload(){
        var d = new Date();
        var n = d.getHours();
        document.getElementById("header").innerHTML = n;

如果。。。否则如果...其他声明用于问候的时间

    if (new Date().getHours() < 12) {
    document.getElementById("header").innerHTML = "Enjoy the rest of your morning!";
    } else if (new Date().getHours() < 17) {
    document.getElementById("header").innerHTML = "Enjoy the afternoon!";
    } else  (new Date().getHours() > 17) 
    document.getElementById("header").innerHTML = "Have a good evening!";

这些变量应该使代码可以显示在我的标头

    var firstVariable = document.createElement("H3"); 
    var secondVariable = document.createTextNode(n);
    firstVariable.appendChild(secondVariable);
    document.body.appendChild(firstVariable); 
    }

    </script>

结束 JavaScript

    </div>
    </body>
    </head>

你需要像这样处理你的代码} else (new Date().getHours() > 17)

尝试以下代码:

<html>
 
  
<head>
</head>
  
  <body onload="myOnload()">
<div id="header"></div>
    <div id=img><img src="resumeheader.png" alt="Header" style="width:750px;height:100px;"></div><div id=h2>
    <h2>Pay Calculator</h2>
    <h3></h3></div>
<script>
function myOnload(){
        var d = new Date();
        var n = d.getHours();
        document.getElementById("header").innerHTML = n;
  if (n < 12) {
    document.getElementById("header").innerHTML = "Enjoy the rest of your morning!";
    } else if (n < 17 && n > 12) {
    document.getElementById("header").innerHTML = "Enjoy the afternoon!";
    } else {
    document.getElementById("header").innerHTML = "Have a good evening!";
    }
    var firstVariable = document.createElement("H3"); 
    var secondVariable = document.createTextNode(n);
    firstVariable.appendChild(secondVariable);
    document.body.appendChild(firstVariable); 
    }
  </script>