document.getElementById 根本不工作
document.getElementById is not working at all
我是一个巨大的北欧神话迷,我正在尝试制作 Ragnarok 倒计时 Chrome 应用程序。该代码在一个编辑网站上运行良好,即使我将它放在 post 中,但当我尝试将其作为应用程序打开时,用于显示时钟的 document.getElementById 没有任何作用,不管我做什么,我都做了。这是代码:
<!DOCTYPE HTML>
<html>
<head>
<style>
p {
text-align: center;
font-size: 60px;
}
body {
color:white;
background-color:gray;
</style>
</head>
<body>
<p id="demo"></p>
<script>
window.onload = (function () {// Set the date we're counting down to
var countDownDate = new Date("Jan 1, 2099 12:00:00").getTime();
// Update the count down every 1 second
var x = setInterval(function() {
// Get todays date and time
var now = new Date().getTime();
// Find the distance between now an the count down date
var distance = countDownDate - now;
// Time calculations for days, hours, minutes and seconds
var years = Math.floor(distance / (1000 * 60 * 60 * 24 * 365));
var days = Math.floor((distance % (1000 * 60 * 60 * 24 * 365)) / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Output the result in an element with id="demo"
document.getElementById("demo").innerHTML = years + "y" + days + "d " + hours + "h "
+ minutes + "m " + seconds + "s ";
// If the count down is over, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "Prepare for Ragnarok";
}
}, 1000);});
</script>
</body>
</html>
评论中的建议是正确的。将标签内的所有 Javascript 移动到一个单独的文件中,比如 script.js。然后将您的脚本标签替换为引用该文件的标签:
<script src="script.js"></script>
我是一个巨大的北欧神话迷,我正在尝试制作 Ragnarok 倒计时 Chrome 应用程序。该代码在一个编辑网站上运行良好,即使我将它放在 post 中,但当我尝试将其作为应用程序打开时,用于显示时钟的 document.getElementById 没有任何作用,不管我做什么,我都做了。这是代码:
<!DOCTYPE HTML>
<html>
<head>
<style>
p {
text-align: center;
font-size: 60px;
}
body {
color:white;
background-color:gray;
</style>
</head>
<body>
<p id="demo"></p>
<script>
window.onload = (function () {// Set the date we're counting down to
var countDownDate = new Date("Jan 1, 2099 12:00:00").getTime();
// Update the count down every 1 second
var x = setInterval(function() {
// Get todays date and time
var now = new Date().getTime();
// Find the distance between now an the count down date
var distance = countDownDate - now;
// Time calculations for days, hours, minutes and seconds
var years = Math.floor(distance / (1000 * 60 * 60 * 24 * 365));
var days = Math.floor((distance % (1000 * 60 * 60 * 24 * 365)) / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Output the result in an element with id="demo"
document.getElementById("demo").innerHTML = years + "y" + days + "d " + hours + "h "
+ minutes + "m " + seconds + "s ";
// If the count down is over, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "Prepare for Ragnarok";
}
}, 1000);});
</script>
</body>
</html>
评论中的建议是正确的。将标签内的所有 Javascript 移动到一个单独的文件中,比如 script.js。然后将您的脚本标签替换为引用该文件的标签:
<script src="script.js"></script>