无法使用 jQuery 访问关联数组中的对象
我正在尝试创建对象数组,以便我可以在 jQuery 中的 for 循环中访问它们,并且我知道这在 Actionscript 中有效,所以我想做的是将我当前的知识转换为可以工作的 jQuery。
请看一下这个并告诉我为什么我无法访问 divToShow
谢谢大家
var homeImages = new Array();
homeImages[0] = { hoverImage: ".leftColImage1", divToShow: ".image1", rollOverImg: "img-family-over.jpg" };
homeImages[1] = { hoverImage: ".leftColImage2", divToShow: ".image2", rollOverImg: "img-students-over.jpg" };
homeImages[2] = { hoverImage: ".leftColImage3", divToShow: ".image3", rollOverImg: "img-pros-over.jpg" };
homeImages[3] = { hoverImage: ".leftColImage4", divToShow: ".image4", rollOverImg: "img-retired-over.jpg" };
var hoverImage;
var activeDiv;
var mainContent = ".mainContent";
for (k = 0; k < homeImages.length; k++) {
homeImages[k].id = k;
$(homeImages[k].hoverImage).mouseover(function() {
//alert("divToShow : " + homeImages[this.id].divToShow);
alert("this : " + this.id);
activeDiv = homeImages[k].divToShow;
$(".leftColImage1 img").attr("src", "/App_Themes/MyChoice2010/Images/" + homeImages[k].rollOverImg);
$(mainContent).hide();
$(homeImages[k].divToShow).slideDown("slow");
}).mouseout(function() {
$(".leftColImage1 img").attr("src", "/App_Themes/MyChoice2010/Images/img-family.jpg");
$(".image1").hide();
$(mainContent).slideDown("slow");
});
}
I am trying to create and array of objects so that I can access them in a for loop in jQuery and I know that this works in Actionscript so what I am trying to do is convert my current knowledge to jQuery that will work.
Please have a look at this and tell me why I cannot access divToShow
Thanks guys
var homeImages = new Array();
homeImages[0] = { hoverImage: ".leftColImage1", divToShow: ".image1", rollOverImg: "img-family-over.jpg" };
homeImages[1] = { hoverImage: ".leftColImage2", divToShow: ".image2", rollOverImg: "img-students-over.jpg" };
homeImages[2] = { hoverImage: ".leftColImage3", divToShow: ".image3", rollOverImg: "img-pros-over.jpg" };
homeImages[3] = { hoverImage: ".leftColImage4", divToShow: ".image4", rollOverImg: "img-retired-over.jpg" };
var hoverImage;
var activeDiv;
var mainContent = ".mainContent";
for (k = 0; k < homeImages.length; k++) {
homeImages[k].id = k;
$(homeImages[k].hoverImage).mouseover(function() {
//alert("divToShow : " + homeImages[this.id].divToShow);
alert("this : " + this.id);
activeDiv = homeImages[k].divToShow;
$(".leftColImage1 img").attr("src", "/App_Themes/MyChoice2010/Images/" + homeImages[k].rollOverImg);
$(mainContent).hide();
$(homeImages[k].divToShow).slideDown("slow");
}).mouseout(function() {
$(".leftColImage1 img").attr("src", "/App_Themes/MyChoice2010/Images/img-family.jpg");
$(".image1").hide();
$(mainContent).slideDown("slow");
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好的,试试这个:
或者:
Ok, try this:
Or alternatively:
在这种情况下,
this
指的是当前 HTML 元素,而不是当前的 homeImages 元素。In that context,
this
refers to the current HTML element, not the current homeImages element.您正在从鼠标悬停处理函数内部访问变量
k
。但在调用该函数时,k
的值已经发生变化,现在等于homeImages.length
,因为for
循环已经已经运行完成。解决这个问题的一种方法是使用
$.each
而不是 for 循环:这会起作用,因为传递给
$.each
的函数会创建一个新的闭包来记住该值每次迭代的k
。You are accessing the variable
k
from inside the mouseover handler function. But by the time that function is called, the value ofk
has already changed and is now equal tohomeImages.length
since thefor
loop has already run to completion.One way to solve this is to use
$.each
instead of the for loop:This will work because the function passed to
$.each
creates a new closure which remembers the value ofk
for each iteration.原因是与闭包相关的旧经典:在 mouseover 处理程序中,
k
始终设置为其最后一个值 4,而不是创建 mouseover 处理程序时的值,这正是您的代码所期望的。您可以通过在函数中创建鼠标悬停处理程序来解决此问题:
The reason is the old classic relating to closures: in the mouseover handler,
k
is always set to its last value of 4 rather than its value when the mouseover handler was created, which is what your code is expecting.You can fix this by creating the mouseover handler in a function: