daz*_*aze 0 html javascript jquery
我刚开始使用HTML/CSS/jQuery,我尝试开始基本的.目前我有.html,.css和.js.我的index.html将加载.css但不会加载.js.我的代码有问题吗?
<!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>
Run Code Online (Sandbox Code Playgroud)
img {
position: relative;
left: 100px;
top: 100px;
}
Run Code Online (Sandbox Code Playgroud)
$(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;
}
});
});
Run Code Online (Sandbox Code Playgroud)
请帮我弄清楚为什么这不起作用.一些额外的信息,它们都位于我桌面上的同一文件夹中.没有子文件夹.
您在JavaScript文件中使用jQuery代码,但不在HTML中包含jquery Lib.
在加载脚本之前添加此行(或者您可以使用链接到托管jQuery库的其他链接)(假设您使用当前的jQuery版本 - 否则包括相应的版本):
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
Run Code Online (Sandbox Code Playgroud)
所以你<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 -->
Run Code Online (Sandbox Code Playgroud)