我该怎么做才能让这段代码按照我的意愿运行

时间:2014-05-20 10:17:15

标签: javascript

我已经制作了一个应用程序,其中向用户呈现了一堆图像,在那里他点击图像并根据该图像呈现另一个图像,例如

male->body_type_a->below_18->recommended_a
male->body_type_b->below_18->recommended_b
male->body_type_c->below_18->recommended_c
male->body_type_a->above_18->recommended_a
male->body_type_b->above_18->recommended_b
male->body_type_c->above_18->recommended_c
male->body_type_a->above_35->recommended_a
male->body_type_b->above_35->recommended_b
male->body_type_c->above_35->recommended_c

对于女性来说,现在我已经粘贴了这段代码,如何知道用户点击了哪个图像,以便在最后一步之后我可以根据他点击的内容来定位网址,我就是这样。我无法获取用户点击的图片,我已将代码粘贴到jsfiddle中点击此处view抱歉代码更大

1 个答案:

答案 0 :(得分:0)

由于选择类型相似,您可以将三个函数折射成一个,并确定单击图像所采用的路径。

var IMG_MALE = "http://placehold.it/50x50/343434&text=Male";
var IMG_FEMALE = "http://placehold.it/50x50/dedede&text=Female";

var IMG_ECTO = "http://placehold.it/50x50/343434&text=Ecto";
var IMG_ENDO = "http://placehold.it/50x50/343434&text=Endo";
var IMG_MESO = "http://placehold.it/50x50/343434&text=Meso";

var IMG_AGE_18 = "http://placehold.it/50x50/343434&text=-18";
var IMG_AGE_1835 = "http://placehold.it/50x50/343434&text=18-35";
var IMG_AGE_35 = "http://placehold.it/50x50/343434&text=35+";

var IMG_PROD_BULK = "http://placehold.it/50x50/343434&text=Bulk";
var IMG_PROD_FIT = "http://placehold.it/50x50/343434&text=Fit";
var IMG_PROD_MUSCLE = "http://placehold.it/50x50/343434&text=Muscle";

$(document).ready(function () {
    var state = 0;
    var link = new Array(4);
    var male = "";

    // using images that have id starting with '#img' can narrow if required
    $("img").click(function (e) {
        $("img").removeClass('animated fadeIn');

        var src = $(e.target).attr("src");

        switch (src) {
            case IMG_MALE:
                link[0] = src;
                $('#runtime-text').text("SELECT YOUR BODY TYPE :");
                $('#img1').attr("src", IMG_ECTO);
                $('#img2').attr("src", IMG_ENDO);
                $('#img3').attr("src", IMG_MESO);
                break;

            case IMG_ECTO:
            case IMG_ENDO:
            case IMG_MESO:
                link[1] = src;
                $('#runtime-text').text("SELECT YOUR AGE :");
                $('#img1').attr("src", IMG_AGE_18);
                $('#img2').attr("src", IMG_AGE_1835);
                $('#img3').attr("src", IMG_AGE_35);
                break;

            case IMG_AGE_18:
            case IMG_AGE_1835:
            case IMG_AGE_35:
                link[2] = src;
                $('#runtime-text').text("SELECT PRODUCT FOR :");
                $('#img1').attr("src", IMG_PROD_BULK);
                $('#img2').attr("src", IMG_PROD_FIT);
                $('#img3').attr("src", IMG_PROD_MUSCLE);
                break;

            case IMG_PROD_BULK:
            case IMG_PROD_FIT:
            case IMG_PROD_MUSCLE:
                link[3] = src;
                alert("User selected " + link[0] + " -> " + link[1] + " -> " + link[2] + " -> " + link[3]);
                $('#btn1').click();
                break;

            default:
                break;
        }

        $("img").addClass('animated fadeIn');
    })

    $('#btn1').click(function () {
        console.log("btn");
        $('#img1').attr("src", IMG_MALE);
        $('#img2').attr("src", IMG_FEMALE);
        $('#img3').attr("src", "");

        $("img").addClass('animated fadeIn');
    })
});

上面的代码使用了一般的img选择器,你可以通过使用正确的类等切换到更具体的。结果当前被提醒屏幕,但它可以用来确定用户采取的路径。 jsFiddle at - http://jsfiddle.net/sRD5r/