在onClick方法中调用JavaScript函数

时间:2015-09-28 17:28:41

标签: javascript html

我有这个功能

    function setLang(ln){
    if(ln == "en"){
        window.location = "../../en/page/login.html";
    }else if(ln == "de"){
        window.location = "../../de/page/login.html";
    }else if(ln == "fr"){
        window.location = "../../fr/page/login.html";
    }
}

并像这样将其称为内联

<img src="../../en/EN.png" class="flag" onclick="setLang('en')">

所以我想从onclick函数中访问它。

我对JS很陌生,这就是为什么我要问我该怎么做。

1 个答案:

答案 0 :(得分:0)

<img src="../../en/EN.png" class="flag" onclick="setLang('en')"/>
<script>
function setLang(ln){
if(ln == "en"){
    window.location = "../../en/page/login.html";
}else if(ln == "de"){
    window.location = "../../de/page/login.html";
}else if(ln == "fr"){
    window.location = "../../fr/page/login.html";
}
}
</script>