0

我正在制作一个文字游戏来帮助我学习德语。我有 2 个数组,一个用于德语,另一个用于英语。然后,我将所有单词作为“语言”和“位置”类的链接显示到 HTML 页面上,“位置”是一个中间有空格的数字。我想要的是德语和英语单词配对在一起时会淡出。这是我到目前为止的点击事件:

function setup()
{
    var check = { language: undefined, position: null };

    $("a").on("click", function()
    {
        var attributes = $(this).attr("class").split(" ");

        if (attributes[0] == "german") {
            if (check["language"] == "german") {
                alert("German word already clicked!");
                check["language"] = undefined;
                check["position"] = null;
                return;
            }

            if (check["language"] == "english") {
                if (check["position"] == attributes[1]) {
                    $(this).fadeOut(800);
                }
            }

            check["language"] = attributes[0];
            check["position"] = attributes[1];
        }

        if (attributes[0] == "english") {
            if (check["language"] == "english") {
                alert("English word already clicked!");
                check["language"] = undefined;
                check["position"] = null;
                return;
            }

            if (check["language"] == "german") {
                if (check["position"] == attributes[1]) {
                    $(this).fadeOut(800);
                }
            }

            check["language"] = attributes[0];
            check["position"] = attributes[1];
        }
    });
}

编辑:

function wordlist(obj)
{
    for (i = 0; i < obj["wordlist"].length; i ++) {
        $("#main .span12").append("<a href=\"#\" class=\"" + obj["language"] + " " + i + "\">" + obj["wordlist"][i] + "</a><br/>");
    }
}

全面披露:

我已经完成了我的文字游戏,所以我决定通过提交我的最终版本来回馈社区,以便其他人可以使用。该游戏还支持随机词表,然后将它们合并在一起。如果您认为可以改进代码,请发表评论。

$(document).ready(function()
{
    (function training()
    {
        Array.prototype.shuffle = function()
        {
            var i = this.length, j, temp;
            if (i == 0) return this;

            while (--i) {
                j = Math.floor(Math.random() * (i + 1));
                temp = this[i];
                this[i] = this[j];
                this[j] = temp;
            }
            return this;
        }

        var german =
        {
            language: "german",
            wordlist:
            [
                "der Zusammenhang",
                "der Wirt",
                "der Kaufmann",
                "das Gesetz",
                "(sich) klammern (an)",
                "der Lastwagen",
                "die Akte",
                "das Gericht",
                "zahlen",
                "zählen (bis, auf)"
            ]
        },
        english =
        {
            language: "english",
            wordlist:
            [
                "connection",
                "landlord",
                "dealer",
                "law",
                "to attach (to)",
                "truck",
                "file",
                "dish",
                "to pay",
                "to count (to, on)"
            ]
        };

        function generate(obj)
        {
            var array = [];

            for (i = 0; i < obj["wordlist"].length; i ++) {
                array.push("<a href=\"#\" class=\"" + obj["language"] + " " + i + "\">" + obj["wordlist"][i] + "</a><br/>");
            }
            return array;
        }

        var german = generate(german);
        var english = generate(english);

        var wordlist = german.concat(english);
        wordlist.shuffle();

        $("#main .span12").append(wordlist);

        (function setup()
        {
            var check = { language: undefined, position: null };

            $("a").on("click", function()
            {
                var attributes = $(this).attr("class").split(" ");

                if (attributes[0] == "german") {
                    if (check["language"] == "german") {
                        alert("    German word clicked!\nPlease click an English word.");
                        check["language"] = undefined;
                        check["position"] = null;
                        return;
                    }

                    if (check["language"] == "english") {
                        if (check["position"] == attributes[1]) {
                            $("." + attributes[1]).fadeOut(800);
                            $("." + attributes[1]).remove();
                        }
                    }

                    check["language"] = attributes[0];
                    check["position"] = attributes[1];
                }

                if (attributes[0] == "english") {
                    if (check["language"] == "english") {
                        alert("      English word clicked!\nPlease click a German word.");
                        check["language"] = undefined;
                        check["position"] = null;
                        return;
                    }

                    if (check["language"] == "german") {
                        if (check["position"] == attributes[1]) {
                            $("." + attributes[1]).fadeOut(800);
                            $("." + attributes[1]).remove();
                        }
                    }

                    check["language"] = attributes[0];
                    check["position"] = attributes[1];
                }
            });

            (function loop()
            {
                var timer = setTimeout(loop, 1000);
                var links = $("a");

                if (links.length == 0) {
                    clearTimeout(timer);
                    alert("Well done!");
                    return;
                }
            })();
        })();
    })();
});
4

2 回答 2

0

我认为这应该有效。

function setup()
{
var check = { language: undefined, position: null };
$("a").on("click", function()
{
    var attributes = $(this).attr("class").split(" ");

    if (attributes[0] == "german") {
        if (check["language"] == "german") {
            alert("German word already clicked!");
            check["language"] = undefined;
            check["position"] = null;
            return;
        }

        if (check["language"] == "english") {
                $("a." + check["language"]).fadeOut(800);
                $("a").not("a." + check["language"]).fadeIn(800);
        }

        check["language"] = attributes[0];
        check["position"] = attributes[1];
    }

    if (attributes[0] == "english") {
        if (check["language"] == "english") {
            alert("English word already clicked!");
            check["language"] = undefined;
            check["position"] = null;
            return;
        }

        if (check["language"] == "german") {
                $("a." + check["language"]).fadeOut(800);
                $("a").not("a." + check["language"]).fadeIn(800);
        }

        check["language"] = attributes[0];
        check["position"] = attributes[1];
    }
});
}
于 2013-07-24T09:38:41.717 回答
0

替换

$(this).fadeOut(800);  

$("."+attributes[1]).fadeOut(800); 
OR
$("."+check["position"]).fadeOut(800);
于 2013-07-24T09:47:16.273 回答