无法使用 jQuery 访问关联数组中的对象

发布于 2024-09-03 11:25:10 字数 1394 浏览 2 评论 0原文

我正在尝试创建对象数组,以便我可以在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

若水般的淡然安静女子 2024-09-10 11:25:10

好的,试试这个:

for (k = 0; k < homeImages.length; k++) {
    (function(current) {
        $(current.hoverImage).hover(function() {
            activeDiv = current.divToShow;
            $(".leftColImage1 img").attr("src", "/App_Themes/MyChoice2010/Images/" + current.rollOverImg);
            $(mainContent).hide();
            $(current.divToShow).slideDown("slow");
        },
        function() {
            $(".leftColImage1 img").attr("src", "/App_Themes/MyChoice2010/Images/img-family.jpg");
            $(".image1").hide();
            $(mainContent).slideDown("slow");
        });
    })(homeImages[k]);
}

或者:

function setUpHover(item) {
    $(item.hoverImage).hover(function() {
        activeDiv = item.divToShow;
        $(".leftColImage1 img").attr("src", "/App_Themes/MyChoice2010/Images/" + current.rollOverImg);
        $(mainContent).hide();
        $(item.divToShow).slideDown("slow");
    },
    function() {
        $(".leftColImage1 img").attr("src", "/App_Themes/MyChoice2010/Images/img-family.jpg");
        $(".image1").hide();
        $(mainContent).slideDown("slow");
    });
}

for (k = 0; k < homeImages.length; k++) {
    setUpHover(homeImages[k]);
}

Ok, try this:

for (k = 0; k < homeImages.length; k++) {
    (function(current) {
        $(current.hoverImage).hover(function() {
            activeDiv = current.divToShow;
            $(".leftColImage1 img").attr("src", "/App_Themes/MyChoice2010/Images/" + current.rollOverImg);
            $(mainContent).hide();
            $(current.divToShow).slideDown("slow");
        },
        function() {
            $(".leftColImage1 img").attr("src", "/App_Themes/MyChoice2010/Images/img-family.jpg");
            $(".image1").hide();
            $(mainContent).slideDown("slow");
        });
    })(homeImages[k]);
}

Or alternatively:

function setUpHover(item) {
    $(item.hoverImage).hover(function() {
        activeDiv = item.divToShow;
        $(".leftColImage1 img").attr("src", "/App_Themes/MyChoice2010/Images/" + current.rollOverImg);
        $(mainContent).hide();
        $(item.divToShow).slideDown("slow");
    },
    function() {
        $(".leftColImage1 img").attr("src", "/App_Themes/MyChoice2010/Images/img-family.jpg");
        $(".image1").hide();
        $(mainContent).slideDown("slow");
    });
}

for (k = 0; k < homeImages.length; k++) {
    setUpHover(homeImages[k]);
}
溺深海 2024-09-10 11:25:10
//alert("divToShow : " + homeImages[this.id].divToShow);

在这种情况下,this 指的是当前 HTML 元素,而不是当前的 homeImages 元素。

//alert("divToShow : " + homeImages[this.id].divToShow);

In that context, this refers to the current HTML element, not the current homeImages element.

猫性小仙女 2024-09-10 11:25:10

您正在从鼠标悬停处理函数内部访问变量k。但在调用该函数时,k 的值已经发生变化,现在等于 homeImages.length,因为 for 循环已经已经运行完成。

解决这个问题的一种方法是使用 $.each 而不是 for 循环:

$.each(homeImages, function(k, element) {
    element.id = k;
    $(element.hoverImage).mouseOver(function() {
        .... //you can use the value of k or element here
    });
});

这会起作用,因为传递给 $.each 的函数会创建一个新的闭包来记住该值每次迭代的 k

You are accessing the variable k from inside the mouseover handler function. But by the time that function is called, the value of k has already changed and is now equal to homeImages.length since the for loop has already run to completion.

One way to solve this is to use $.each instead of the for loop:

$.each(homeImages, function(k, element) {
    element.id = k;
    $(element.hoverImage).mouseOver(function() {
        .... //you can use the value of k or element here
    });
});

This will work because the function passed to $.each creates a new closure which remembers the value of k for each iteration.

北恋 2024-09-10 11:25:10

原因是与闭包相关的旧经典:在 mouseover 处理程序中,k 始终设置为其最后一个值 4,而不是创建 mouseover 处理程序时的值,这正是您的代码所期望的。

您可以通过在函数中创建鼠标悬停处理程序来解决此问题:

function addMouseEventHandlers(imageIndex) {
    var homeImage = homeImages[imageIndex];
    homeImage.id = imageIndex;
    $(homeImage.hoverImage).mouseover(function() {
    //alert("divToShow : " + homeImages[this.id].divToShow);
        alert("this : " + this.id);
        activeDiv = homeImage.divToShow;
        $(".leftColImage1 img").attr("src", "/App_Themes/MyChoice2010/Images/" + homeImage.rollOverImg);
        $(mainContent).hide();
        $(homeImage.divToShow).slideDown("slow");
    }).mouseout(function() {
        $(".leftColImage1 img").attr("src", "/App_Themes/MyChoice2010/Images/img-family.jpg");
        $(".image1").hide();
        $(mainContent).slideDown("slow");
    });
}

for (k = 0; k < homeImages.length; k++) {
    addMouseEventHandlers(k);
}

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:

function addMouseEventHandlers(imageIndex) {
    var homeImage = homeImages[imageIndex];
    homeImage.id = imageIndex;
    $(homeImage.hoverImage).mouseover(function() {
    //alert("divToShow : " + homeImages[this.id].divToShow);
        alert("this : " + this.id);
        activeDiv = homeImage.divToShow;
        $(".leftColImage1 img").attr("src", "/App_Themes/MyChoice2010/Images/" + homeImage.rollOverImg);
        $(mainContent).hide();
        $(homeImage.divToShow).slideDown("slow");
    }).mouseout(function() {
        $(".leftColImage1 img").attr("src", "/App_Themes/MyChoice2010/Images/img-family.jpg");
        $(".image1").hide();
        $(mainContent).slideDown("slow");
    });
}

for (k = 0; k < homeImages.length; k++) {
    addMouseEventHandlers(k);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文