1

第 35 行,就在警报之前,返回 -1。我也尝试$(this).index()过同样的结果。这是它应该做的:单击EN.gif应该返回 4,然后grand_array_pics[4]应该给我en_array_pics并加载该.gifs数组。

$(document).ready(function () {

    var main_pics = ["AN.gif", "BN.gif", "CN.gif", "DN.gif", "EN.gif", "GN.gif"];

    var starting_pics = ["AN.gif", "CN.gif", "EN.gif"];

    var an_array_pics = ["BN.gif", "EN.gif", "GN.gif", "AN.gif","DN.gif"];
    var bn_array_pics = ["CN.gif", "DN.gif", "GN.gif"];
    var cn_array_pics = ["DN.gif", "GN.gif", "AN.gif", "CN.gif"];
    var dn_array_pics = ["EN.gif", "AN.gif", "CN.gif"];
    var en_array_pics = ["GN.gif", "AN.gif", "CN.gif", "EN.gif"];
    var gn_array_pics = ["AN.gif", "CN.gif", "EN.gif", "GN.gif"];

    var grand_array_pics = [
        an_array_pics,
        bn_array_pics,
        cn_array_pics,
        dn_array_pics,
        en_array_pics,
        gn_array_pics
    ];

    var i = 0;

    for (i = 0; i < starting_pics.length; i++) {
        $("<img/>").attr("src", "images/" + starting_pics[i]).load(function () {
            $(this).appendTo("#main");
            $(this).addClass("pics");
        });
    }

    $("#main").on("click", ".pics", function () {

        var j = $.inArray(this, main_pics);
        alert(j);

        $("#sidebar .pics").remove();
        $(this).clone().appendTo("#train");
        $(this).clone().appendTo("#sidebar");
        $("#main .pics").remove();

        var chosen_pics_array = grand_array_pics[j];
        var count = chosen_pics_array.length;

        var k = 0;

        for (k = 0; k < count; k++) {
            $("<img/>").attr("src", "images/" + chosen_pics_array[k]).load(function () {
                $(this).appendTo("#main");
                $(this).addClass("pics");
            });
        }
    });
});       //end ready
4

2 回答 2

1

试试这个。您需要获取文件的名称,并将元素本身传递给$.inArray

var j = $.inArray(this.src.substring(this.src.lastIndexOf('/')+1), main_pics);
于 2013-07-31T19:26:59.290 回答
1

this是 DOM<img>元素,main_pics而是字符串数组。它永远不会在那里被发现。利用

var j = $.inArray(this.src.split("/").pop(), main_pics);
于 2013-07-31T19:27:29.903 回答