从函数内部引用存储返回的 jQuery 对象的全局变量
好吧,我知道这里发生了一些简单的事情,我错过了,我想做的就是存储从 jQuery 查询返回的对象,如
var obj = $("#objectID");
作为全局变量,然后从函数内引用它。
下面的示例提供了一张狗的图片,但随后使用 jQuery 交换图像的 src 属性以显示一只猫。我缺少什么?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<!-- jquery -->
<script src="jquery-1.6.1.min.js" type="text/javascript" language="javascript"></script>
<!-- image swap -->
<script type="text/javascript" language="javascript">
// a global jQuery object
var globalMainImage = $("#mainImage");
// a global integer
var globalInteger = 420;
$(document).ready(function() {
/* this works
$("#mainImage").hide();
$("#mainImage").attr("src", "http://upload.wikimedia.org/wikipedia/commons/thumb/f/f3/Youngkitten.JPG/800px-Youngkitten.JPG");
$("#mainImage").fadeIn(1500);
*/
/* and this works
var localMainImage = $("#mainImage");
localMainImage.hide();
localMainImage.attr("src", "http://upload.wikimedia.org/wikipedia/commons/thumb/f/f3/Youngkitten.JPG/800px-Youngkitten.JPG");
localMainImage.fadeIn(1500);
*/
/* and this works
alert(globalInteger);
*/
/* but this doesn't */
globalMainImage.hide();
globalMainImage.attr("src", "http://upload.wikimedia.org/wikipedia/commons/thumb/f/f3/Youngkitten.JPG/800px-Youngkitten.JPG");
globalMainImage.fadeIn(1500);
});
</script>
</head>
<body>
<img id="mainImage" src="http://upload.wikimedia.org/wikipedia/commons/thumb/2/26/YellowLabradorLooking_new.jpg/250px-YellowLabradorLooking_new.jpg" />
</body>
</html>
Ok, I know there is something simple going on here that I'm missing, all I'm trying to do is store an object returned from a jQuery query like
var obj = $("#objectID");
as a global vand then reference it from within a function.
The example below serves a picture of a dog but then uses jQuery to swap the src attribute of the image to show a cat. What am I missing?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<!-- jquery -->
<script src="jquery-1.6.1.min.js" type="text/javascript" language="javascript"></script>
<!-- image swap -->
<script type="text/javascript" language="javascript">
// a global jQuery object
var globalMainImage = $("#mainImage");
// a global integer
var globalInteger = 420;
$(document).ready(function() {
/* this works
$("#mainImage").hide();
$("#mainImage").attr("src", "http://upload.wikimedia.org/wikipedia/commons/thumb/f/f3/Youngkitten.JPG/800px-Youngkitten.JPG");
$("#mainImage").fadeIn(1500);
*/
/* and this works
var localMainImage = $("#mainImage");
localMainImage.hide();
localMainImage.attr("src", "http://upload.wikimedia.org/wikipedia/commons/thumb/f/f3/Youngkitten.JPG/800px-Youngkitten.JPG");
localMainImage.fadeIn(1500);
*/
/* and this works
alert(globalInteger);
*/
/* but this doesn't */
globalMainImage.hide();
globalMainImage.attr("src", "http://upload.wikimedia.org/wikipedia/commons/thumb/f/f3/Youngkitten.JPG/800px-Youngkitten.JPG");
globalMainImage.fadeIn(1500);
});
</script>
</head>
<body>
<img id="mainImage" src="http://upload.wikimedia.org/wikipedia/commons/thumb/2/26/YellowLabradorLooking_new.jpg/250px-YellowLabradorLooking_new.jpg" />
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
var globalMainImage = $("#mainImage");
位于 document.ready 之外,执行此代码时该元素不存在。这样做:
var globalMainImage = $("#mainImage");
is outside of document.ready, the element doesnt' exist at the time this code is executed.Do this:
这是因为它是在文档准备好之前计算的。它需要进入 document.ready() 内部。
That's because it calculates that before the document is ready. It needs to go inside the document.ready().