HTML链接到外部JavaScript无法正常工作

时间:2015-08-02 06:13:33

标签: javascript html

我是javascript的新手。我为我的网页创建了一个外部JavaScript,但它不起作用。但是当我在html里面写它时,它运行正常。 这是我的javascript。

window.onload = function(){
document.getElementById("demo").onmouseover = function() {mouseOver()};
document.getElementById("demo").onmouseout = function() {mouseOut()};
function mouseOver(){
    document.getElementById("dem").style.display = "inline";
}
function mouseOut(){
    document.getElementById("dem").style.display = "none";
}
};

这是我的HTML

<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="indexahnew.js"> 
</script>
<link rel="stylesheet" type="text/css" href="first.css">
<div class="container" id='1'><a href="#" class="button" >about me</a>

<a href="www.google.com"; class="button"; id="demo">contact</a>
<a href="www.google.com" class="button" >canvas</a>
<a href="group.html" class="button" >our group</a>
<a href="www.google.com" class="button" >my blog</a></div>

</head>
<body background= 'green.jpg'; >
<div class="contact" style="display:none;" id="dem">
<ul><li>Contact number&nbsp: 1234567890</li>
<li>    Email &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp:email@gmail.com</li>
</ul>
</div>

</body>
</html>

2 个答案:

答案 0 :(得分:0)

从此处更改脚本标记:

<script src="indexahnew.js"> 
</script>

到:

<script src="indexahnew.js" type="text/javascript"></script>

除非您要在页面上添加脚本,否则在开始或结束之间不允许有新的行或空格。

答案 1 :(得分:0)

  1. 检查Chrome开发者以查看是否包含js文件
  2. 路径是否正确?应该是&#39; /indexahnew.js'?
  3. 删除脚本标记中的新行
  4. 将脚本标记添加到html正文的末尾而不是头部(最佳做法)