我刚刚开始使用 HTML/CSS/jQuery,我尝试从基础开始。目前我有 .html、.css 和 .js。我的index.html 将加载.css 但不会加载.js。我的代码有问题吗?
HTML
<!DOCTYPE html>
<html>
<head>
<title>Super Mario!</title>
<link rel='stylesheet' type='text/css' href='stylesheet.css'/>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<img src="http://i1061.photobucket.com/albums/t480/ericqweinstein/mario.jpg"/>
</body>
</html>
CSS
img {
position: relative;
left: 100px;
top: 100px;
}
JQuery
$(document).ready(function() {
$(document).keydown(function(key) {
switch(parseInt(key.which,10)) {
case 65:
$('img').animate({left: "-=10px"}, 'fast');
break;
case 83:
$('img').animate({top: "+=10px"}, 'fast');
break;
case 87:
$('img').animate({top: "-=10px"}, 'fast');
break;
case 68:
$('img').animate({left: "+=10px"}, 'fast');
break;
default:
break;
}
});
});
请帮我找出为什么这不起作用。一些附加信息,它们都位于我桌面上的同一文件夹中。没有子文件夹。
最佳答案
您在 JavaScript 文件中使用 jQuery 代码,但不在 HTML 中包含 jquery Lib。
在加载脚本之前添加此行(或者您可以替换为托管 jQuery 库的另一个链接)(假设您使用当前的 jQuery 版本 - 否则包括适当的版本):
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
所以你的<head>
应该看起来像这样:
<!-- some code -->
<head>
<title>Super Mario!</title>
<link rel='stylesheet' type='text/css' href='stylesheet.css'/>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="script.js"></script>
</head>
<!-- some code -->
关于javascript - 为什么我的 jQuery 脚本无法加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16603284/