为什么我的网页没有显示时间(javaScript)

时间:2014-04-28 01:20:37

标签: javascript html

嘿,伙计们,我已经链接了java脚本文件

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Tech News</title>
    <link href="layout.css" rel="stylesheet" type="text/css" />
    <script type ="text/javascript" src=" script.ja"> </script>

在java脚本文件中我有这段代码

window.onload = initDate;

function initDate()
{
    now = new Date();

    localtime = now.toString();
    globaltime = now.toGMTString();

    document.write("<b> Local time: </b> " + localtime + "<br>");

    hours = now.getHours();
    mins = now.getMinutes();
    secs = now.getSeconds();

    document.write ("<font size = '+1'>");
    document.write(hours + ":" + mins +":" + secs);
    document.write("</font>");

    document.getElementById("time").innerHTML = dtString;
}

现在这段代码应该在div标签中显示字符串,但它不是

    document.getElementById("time").innerHTML = dtString;

这是div标签。

            <div id="time"> <!--Time-->

            </div>

我的网站是100&amp;验证没有编码错误,HTML和CSS。

那会是什么问题?

1 个答案:

答案 0 :(得分:0)

感谢Patrick,我意识到我没有声明dtString。

简单的解决方案是将文本复制到div标签,而不是使事情复杂化,我发现它有用,因为它显示了日期和时间。

                    <div id="date">
                       <script>
                            now = new Date();

                            localtime = now.toString();
                            globaltime = now.toGMTString();

                            document.write(localtime);

                            hours = now.getHours();
                            mins = now.getMinutes();
                            secs = now.getSeconds();

                            document.write ("<font size = '+1'>");
                            document.write(hours + ":" + mins +":" + secs);
                            document.write("</font>");

                        </script>


                    </div>