我不知道为什么,但是当我在http://www.codecademy.com/learn中创建一个JQuery文件时,它确实起作用了;但是当我在altervista.org/上传并运行它时,我就无法理解它为什么不读它!
我首先忘了实现JQuery,但之后我添加了这行代码:
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js?ver=1.4.2'></script>
它似乎仍未被阅读。
我在这里留下我的JS文件,希望它可能会有所帮助:)
$(document).ready(function() {
$(document).keydown(function(key) {
var tleft = $('#char').position().left;
var wleft = $('#rectw').position().left;
var lleft = $('#rectl').position().left;
var lleft_1 = $('#rectl_1').position().left;
var lleft_2 = $('#rectl_2').position().left;
var ttop = $('#char').position().top;
var wtop = $('#rectw').position().top;
var ltop = $('#rectl').position().top;
var ltop_1 = $('#rectl_1').position().top;
var ltop_2 = $('#rectl_2').position().top;
var lost = 0;
var won = 0;
switch(parseInt(key.which,10)) {
// Left arrow key pressed
case 65:
$('#char').animate({left: "-=50px"}, 'fast');
var tleft = $('#char').position().left - 50;
break;
// Up Arrow Pressed
case 87:
$('#char').animate({top: "-=50px"}, 'fast');
var ttop = $('#char').position().top - 50;
break;
// Right Arrow Pressed
case 68:
$('#char').animate({left: "+=50px"}, 'fast');
var tleft = $('#char').position().left + 50;
break;
// Down Arrow Pressed
case 83:
$('#char').animate({top: "+=50px"}, 'fast');
var ttop = $('#char').position().top + 50;
break;
//Jump
case 32:
$('#char').animate({top: "-=50px"}, 'fast');
$('#char').animate({top: "+=50px"}, 'fast');
break;
}
if(tleft === wleft && ttop === wtop){
alert("You Win!");
}
else if(tleft === lleft && ttop === ltop){
alert("You Lose!");
}
else if(tleft === lleft_1 && ttop === ltop_1){
alert("You Lose!");
}
else if(tleft === lleft_2 && ttop === ltop_2){
alert("You Lose!");
}
});
});
// HTML
<head>
<meta charset="utf-8">
<title>Super Mario!</title>
<link rel='stylesheet' type='text/css' href='css.css'/>
<script type="text/javascript" src="scripts.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript">
if (typeof jQuery == 'undefined') {
document.write(unescape("%3Cscript src='inc/js/jquery-1.11.2.min.js' type='text/javascript'%3E%3C/script%3E"));
}
</script>
</head>
答案 0 :(得分:0)
您应下载本地计算机的副本并使用较新版本。然后,您可以创建jQuery源的回退:
<head>
<meta charset="utf-8">
<title>Super Mario!</title>
<link rel='stylesheet' type='text/css' href='css.css'/>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript">
if (typeof jQuery == 'undefined') {
document.write(unescape("%3Cscript src='path/to/jquery-1.11.2.min.js' type='text/javascript'%3E%3C/script%3E"));
}
</script>
<script type='text/javascript' src='script.js'></script>
</head>
如果CDN失败,这个回退将寻找并加载jQuery的本地副本。