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

时间:2016-04-28 21:54:46

标签: javascript html header appendchild

    <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 ... else if else注释以获得问候时间

    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>

1 个答案:

答案 0 :(得分:0)

您需要像这一行} else (new Date().getHours() > 17)

那样处理您的代码

尝试以下代码:

&#13;
&#13;
<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>
&#13;
&#13;
&#13;