这是一个基本问题,但 Google 没有提供任何帮助。
我有一个网站以及能够在其上运行 javascript 的内容。
在我的目录中,有index.html 和index.css。对于 javascript 文件,我假设它应该被称为index.js。
在我的 index.js 文件中,我有这个:
var countTime = 0; // Number of seconds
var redirectURL = "http://example.com"; // URL to direct to
countTime = (countTime+1)*1000;
function updateCount(){
countTime = countTime-1000;
if(document.getElementById("countdownDisplay"))
document.getElementById("countdownDisplay").innerHTML = (countTime/1000);
if(countTime <= 0)
location.href = redirectURL;
else
setTimeout("updateCount()",1000);
}
updateCount();
但是,当我使用浏览器访问该页面时,它不起作用。 我是否必须在 html 文件中执行某些操作,例如包含 index.js 或其他内容?
最佳答案
<script src="index.js" type="text/javascript"></script>
应该放在你的<head>
中.
这将为您加载脚本,然后执行代码。
你还需要类似的东西
<div id="countdownDisplay"></div>
在你的<body>
以便倒计时生效。
当我在做的时候,你可能想要一个
<style src="index.css" type="text/css"></style>
在你的<head>
如果您还没有这样做的话也是如此。
关于网站中的 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5334094/