2016-04-19 59 views
3

请帮忙,我是新来的网页设计,并希望使下面的演示工作,但它似乎我的HTML没有链接到我的JavaScript。请查看下面的代码,演示也可在http://jsfiddle.net/xx9ykonc/如何使它工作,我不明白缺少什么。将JavaScript链接到我的HTML

<!DOCTYPE html> 
<html> 

<head> 
<link rel="stylesheet" type="text/css" href="style1.css"> 
</head> 
<body> 

<table> 
    <tr> 
     <td><div>1</div></td> 
     <td><div>2</div></td> 
     <td><div>3</div></td> 
     <td><div>4</div></td> 
    </tr>    
</table> 

<script> 
$('table div') 
    .on('mouseenter', function(){ 
     var div = $(this); 
     div.stop(true, true).animate({ 
      margin: -10, 
      width: "+=20", 
      height: "+=20" 
     }, 'fast'); 
    }) 
    .on('mouseleave', function(){ 
     var div = $(this); 
     div.stop(true, true).animate({ 
      margin: 0, 
      width: "-=20", 
      height: "-=20" 
     }, 'fast'); 
    }) 
</script> 

</body> 
</html> 
+6

到底是什么问题一样吗?链接是什么意思?小提琴似乎按预期工作。 – Utkanos

+1

你希望*发生什么; *发生了什么,以及您在浏览器控制台中获得了哪些(如果有)错误消息(通常可通过按F12获得)? – ArtOfCode

+2

在您的HTML的部分添加。然后脚本应该工作。 – Rocker1985

回答

4

你应该在你的HTML页面

<head><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script></head> 
+0

更具体地说:OP在他/她的'

2

的头@ Rocker1985注释中加入这个脚本包括jQuery的,你可以在jquery文件链接到你的HTML一样,你必须已链接CSS文件。

<head> 
    <link rel="stylesheet" type="text/css" href="style1.css"> 
    <script script src="~/Scripts/jquery-1.10.2.min.js"></script> 
    <script script src="~/Scripts/jquery-ui-1.11.4.min.js"></script> 
</head> 

给出jquery文件的确切路径和版本。

+1

-1。问题中的标签是“javascript”,“jquery”,“html”和“css”。可以假定OP不使用ASP.Net MVC,他使用纯HTML。 “@ Url.Content”语法在纯HTML中不起作用。 – Rocker1985

+0

删除了Downvote :) – Rocker1985

0

在你的HTML你已经错过了jQuery库的链接只需要附上源链接也将工作,你可以检查以下

<!DOCTYPE html> 
<html> 

<head> 
<link rel="stylesheet" type="text/css" href="style1.css"> 
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script> 
</head> 
<body> 

<table> 
    <tr> 
     <td><div>1</div></td> 
     <td><div>2</div></td> 
     <td><div>3</div></td> 
     <td><div>4</div></td> 
    </tr>    
</table> 

<script> 
$('table div') 
    .on('mouseenter', function(){ 
     var div = $(this); 
     div.stop(true, true).animate({ 
      margin: -10, 
      width: "+=20", 
      height: "+=20" 
     }, 'fast'); 
    }) 
    .on('mouseleave', function(){ 
     var div = $(this); 
     div.stop(true, true).animate({ 
      margin: 0, 
      width: "-=20", 
      height: "-=20" 
     }, 'fast'); 
    }) 
</script> 

</body> 
</html>