**完全初学者**为什么我的Javascript不加载?

时间:2017-06-17 16:58:47

标签: javascript

我刚刚学习了html和css后开始学习javascript。

不知何故,剧本没有在我的网站上显示。

<!DOCTYPE html>
<html>
<head>
    <title>Test</title>

    <link rel="stylesheet" href="c01.css">
</head>
<body>
    <script src="addContent.js"></script>
</body>
</html>

和addContent.js脚本

var today = new Date();
var hourNow = new today.getHours();
var greeting;

    if (hourNow > 18) {
        greeting='Good evening!';
    } else if (hourNow > 12) {
        greeting='Good afternoon!';
    } else if (hourNow > 0) {
        greeting='Good Morning!';
    } else {
        greeting ='Welcome!';
    }

document.write('<h1>' + greeting + '</h1>');

我觉得我的问题的答案相当容易,但我找不到问题请帮忙。

提前致谢。

3 个答案:

答案 0 :(得分:1)

您只需要从

行中删除单词new即可
var hourNow = new today.getHours();

这是一个有效的例子

&#13;
&#13;
var today = new Date();
var hourNow = today.getHours();
var greeting;

    if (hourNow > 18) {
        greeting='Good evening!';
    } else if (hourNow > 12) {
        greeting='Good afternoon!';
    } else if (hourNow > 0) {
        greeting='Good Morning!';
    } else {
        greeting ='Welcome!';
    }

document.write('<h1>' + greeting + '</h1>');
&#13;
<html>
<head>
    <title>Test</title>

    <link rel="stylesheet" href="c01.css">
</head>
<body>
    <script src="addContent.js"></script>
</body>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

简单地说,看看:

var today = new Date(); // new is the root of you date.

然后当你已经定义了root new时,你就没有再次使用了:

var hourNow = new today.getHours();

这是正确的代码:

var hourNow = today.getHours(); // Use this code.

答案 2 :(得分:0)

从var hourNow = new today.getHours()

中删除新内容

&#13;
&#13;
var today = new Date();
var hourNow = today.getHours();
var greeting;

    if (hourNow > 18) {
        greeting='Good evening!';
    } else if (hourNow > 12) {
        greeting='Good afternoon!';
    } else if (hourNow > 0) {
        greeting='Good Morning!';
    } else {
        greeting ='Welcome!';
    }

document.write('<h1>' + greeting + '</h1>');
&#13;
&#13;
&#13;