我的JS语言检测程序返回错误结果

时间:2018-12-29 13:06:55

标签: javascript

我使用以下代码:

function Lang() {
    document.getElementById("en-SG").lang = "en-SG";
    document.getElementById("ja-JP").lang = "ja-JP";
    var language = navigator.userLanguage || navigator.language;
    var lang = new URL(document.location).searchParams.get("lang");
    if (lang === "ja-JP" || "ja") {
        document.getElementById("ja-JP").style.display = "initial";
    } else {
        if (lang === "en-SG" || "en") {
            document.getElementById("en-SG").style.display = "initial";
        } else {
            if (language === "ja-JP" || "ja") {
                document.getElementById("ja-JP").style.display = "initial";
            } else {
                document.getElementById("en-SG").style.display = "initial";
            }
            var a;
        }
        a = 0;
    }
    a = 0;
}

,并希望“ en-SG” div显示URL中是否没有参数,因为我的手机和浏览器语言设置中都没有“ ja-JP”或“ ja”。但是,如果不输入参数,则会显示“ ja-JP” div。但是,如果我用if (lang === "ja-JP")if (lang === "ja")代替if (lang === "ja-JP" || "ja")if (lang === "ja" || "ja-JP"),并且如果我用if (language === "ja-JP")(if language === "ja")代替{{1} }或if (language === "ja-JP" || "ja"),它可以工作。有人可以告诉我我在做什么错吗?顺便说一下,if (language === "ja" || "ja-JP")var a在那里,所以我可以正确地加载脚本,并且CSS代码中有a = 0

0 个答案:

没有答案