如何在Google App Engine中使用Google Libraries API (jQuery)?
我想在我的网站上添加这个jQuery悬停效果。我没有下载jQuery,而是选择使用Google的库API来引入jQuery。我获取了一个密钥,并把需要放在网页头部的代码复制过去,然后又复制了悬停效果的jQuery代码,但什么都没有发生。我到底哪里出错了呢?这是我的测试页面,下面是完整的代码和处理程序:
class JQueryTest(webapp.RequestHandler):
def get(self):
self.response.out.write("""<html>""")
self.response.out.write("""<head>""")
self.response.out.write("""<script type="text/javascript" src="https://www.google.com/jsapi?key=GOOGLE LIBRARIES API KEY"></script>""")
self.response.out.write("""<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
$("ul.thumb li").hover(function() {
$(this).css({'z-index' : '10'}); /*Add a higher z-index value so this image stays on top*/
$(this).find('img').addClass("hover").stop() /* Add class of "hover", then stop animation queue buildup*/
.animate({
marginTop: '-110px', /* The next 4 lines will vertically align this image */
marginLeft: '-110px',
top: '50%',
left: '50%',
width: '174px', /* Set new width */
height: '174px', /* Set new height */
padding: '20px'
}, 200); /* this value of "200" is the speed of how fast/slow this hover animates */
} , function() {
$(this).css({'z-index' : '0'}); /* Set z-index back to 0 */
$(this).find('img').removeClass("hover").stop() /* Remove the "hover" class , then stop animation queue buildup*/
.animate({
marginTop: '0', /* Set alignment back to default */
marginLeft: '0',
top: '0',
left: '0',
width: '100px', /* Set width back to default */
height: '100px', /* Set height back to default */
padding: '5px'
}, 400);
});
</script>""")
self.response.out.write("""<link type="text/css" rel="stylesheet"
href="/stylesheets/jquery.css" /> """)
self.response.out.write("""<title>JQuery Test</title>""")
self.response.out.write("</head>")
self.response.out.write("""<body>""")
self.response.out.write("""<div class="content">""")
self.response.out.write("""
<ul class="thumb">
<li><a href="#"><img src="http://ting-1.appspot.com/image?img_id=agZ0aW5nLTFyEAsSCEhvbWVQYWdlGIfcCww" alt="" /></a></li>
<li><a href="#"><img src="http://ting-1.appspot.com/image?img_id=agZ0aW5nLTFyEAsSCEhvbWVQYWdlGIyGCww" alt="" /></a></li>
<li><a href="#"><img src="http://ting-1.appspot.com/image?img_id=agZ0aW5nLTFyEAsSCEhvbWVQYWdlGIm1Cww" alt="" /></a></li>
<li><a href="#"><img src="http://ting-1.appspot.com/image?img_id=agZ0aW5nLTFyEAsSCEhvbWVQYWdlGM-dCww" alt="" /></a></li>
<li><a href="#"><img src="http://ting-1.appspot.com/image?img_id=agZ0aW5nLTFyEAsSCEhvbWVQYWdlGM6dCww" alt="" /></a></li>
<li><a href="#"><img src="http://ting-1.appspot.com/image?img_id=agZ0aW5nLTFyEAsSCEhvbWVQYWdlGIuGCww" alt="" /></a></li>
<li><a href="#"><img src="http://ting-1.appspot.com/image?img_id=agZ0aW5nLTFyEAsSCEhvbWVQYWdlGLrzCww" alt="" /></a></li>
<li><a href="#"><img src="http://ting-1.appspot.com/image?img_id=agZ0aW5nLTFyEAsSCEhvbWVQYWdlGLWlCww" alt="" /></a></li>
<li><a href="#"><img src="http://ting-1.appspot.com/image?img_id=agZ0aW5nLTFyEAsSCEhvbWVQYWdlGLWlCww" alt="" /></a></li>
</ul>""")
self.response.out.write("""</div>""")
self.response.out.write("</body></html>")
1 个回答
3
你没有把你的jQuery代码放在$(document).ready()
里面,这样在你尝试添加事件处理程序的时候,页面的结构(DOM)还没有加载好。一般来说,每当你需要在页面中引用一些元素(而不是仅仅定义一些稍后调用的函数)时,都需要把这些代码放在.ready()
的回调函数里,这样它就不会在DOM完全加载之前运行。试试这样:
$(function() {
$("ul.thumb li").hover(function() {
// ...
}, function() {
// ...
});
});
这样应该就能正常工作了 - 在这里是可以的:http://jsfiddle.net/nrabinowitz/gdsxH/1/