Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何有效地将javascript链接到html?_Javascript_Html - Fatal编程技术网

如何有效地将javascript链接到html?

如何有效地将javascript链接到html?,javascript,html,Javascript,Html,我正在尝试将一个javascript文件链接到我的html,它将显示一个数字时钟 我已经在线检查并使用了脚本标签,正如上面所说的,但是更改没有显示在我的网页上,我需要帮助 html <head> <meta name="viewport" content="width=device-width, initial-scale=1" charset=> <title>About Me</title> &

我正在尝试将一个javascript文件链接到我的html,它将显示一个数字时钟

我已经在线检查并使用了脚本标签,正如上面所说的,但是更改没有显示在我的网页上,我需要帮助

html
 <head>
        <meta name="viewport" content="width=device-width, initial-scale=1" charset=>
        <title>About Me</title>
        <link href=".\main.css" type="text/css" rel="stylesheet">
        <script src="script.js"></script>
    </head>
java脚本

function updateClock(){
    var currentTime = new Date();
    var currentHours = currentTime.getHours();
    var currentMinutes = currentTime.getMinutes();
    var currentSeconds = currentTime.getSeconds();

    currentMinutes = (currentMinutes < 10 ? "0" : "") + currentMinutes;
    currentSeconds = (currentSeconds < 10 ? "0" : "") + currentSeconds;

    var timeOfDay = (currentHours < 12) ? "AM" : "PM";
    currentHours = (currentHours > 12) ? currentHours - 12 : currentHours;
    currentHours = (currentHours == 0) ? 12 : currentHours;

    var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + ":" + timeOfDay;

    document.getElementById('clock').innerHTML = currentTimeString;
};

//windows.onload=init;
在HTML中,尝试将onLoad=updatelock放在body标记或容器标记中,以从JS调用时钟方法

像这样,

这可以用任何标签来完成

完整示例如下:

在HTML中,尝试将onLoad=updatelock放在body标记或容器标记中,以从JS调用时钟方法

像这样,

这可以用任何标签来完成

完整示例如下:


相当多的问题

首先,在您的头脑中,要么将字符集值设置为某个值,要么将其丢弃。我指的问题是

其次,在调用javascript之前需要加载html。要解决此问题,请将脚本放在底部,就在标记之前,或者将脚本更改为。如果您使用第一种方法,请确保调用您的函数;否则,什么也不会发生

最后,您需要添加一个div标记或带有id时钟的东西;否则,您的文档.getElementById'clock'.innerHTML将无法执行任何操作

总摘要:

函数updatelock{ var currentTime=新日期; var currentHours=currentTime.getHours; var currentMinutes=currentTime.getMinutes; var currentSeconds=currentTime.getSeconds; currentMinutes=currentMinutes<10?0:+currentMinutes; currentSeconds=currentSeconds<10?0:+currentSeconds; var timeOfDay=当前小时数<12?上午:下午; currentHours=currentHours>12?currentHours-12:currentHours; 当前小时数=当前小时数==0?12:当前小时数; var currentTimeString=currentHours+:+currentMinutes+:+currentSeconds+:+timeOfDay; document.getElementById'clock'.innerHTML=currentTimeString; }; 关于我
相当多的问题

首先,在您的头脑中,要么将字符集值设置为某个值,要么将其丢弃。我指的问题是

其次,在调用javascript之前需要加载html。要解决此问题,请将脚本放在底部,就在标记之前,或者将脚本更改为。如果您使用第一种方法,请确保调用您的函数;否则,什么也不会发生

最后,您需要添加一个div标记或带有id时钟的东西;否则,您的文档.getElementById'clock'.innerHTML将无法执行任何操作

总摘要:

函数updatelock{ var currentTime=新日期; var currentHours=currentTime.getHours; var currentMinutes=currentTime.getMinutes; var currentSeconds=currentTime.getSeconds; currentMinutes=currentMinutes<10?0:+currentMinutes; currentSeconds=currentSeconds<10?0:+currentSeconds; var timeOfDay=当前小时数<12?上午:下午; currentHours=currentHours>12?currentHours-12:currentHours; 当前小时数=当前小时数==0?12:当前小时数; var currentTimeString=currentHours+:+currentMinutes+:+currentSeconds+:+timeOfDay; document.getElementById'clock'.innerHTML=currentTimeString; }; 关于我
您已将代码放入函数中。您需要调用该函数。您已将代码放入函数中。您需要调用该函数。请注意,此示例具有setTimeout,它将使时钟滴答作响。用户可能希望将其添加到函数中并从正文中调用它,或者简单地使用window.onload=…运行脚本。请注意,此示例具有setTimeout,它将使时钟滴答作响。用户可能希望将其添加到函数中并从正文中调用它,或者只需使用window.onload=。。。
<body onLoad="updateClock()">
....
</body>