如果您只想完整填寫網址並將/en/
替換爲/chi/
或反之亦然,請使用下面的代碼。
HTML
<span onclick="SwitchLang()">View [Some other Language]</span>
的JavaScript
function SwitchLang() {
//Does URL contain "/en/"?
if(window.location.href.indexOf("/en/") != -1) {
//URL contain "/en/", replace with "/chi/"
window.location.href = window.location.href.replace("/en/", "/chi/");
}
//Does URL contain "/chi/"?
else if(window.location.href.indexOf("/chi/") != -1) {
//URL contain "/chi/", replace with "/en/"
window.location.href = window.location.href.replace("/chi/", "/en/");
}
}
或者多一點簡潔(未註釋版本)
function SwitchLang() {
if(window.location.href.indexOf("/en/") != -1)
window.location.href = window.location.href.replace("/en/", "/chi/");
else if(window.location.href.indexOf("/chi/") != -1)
window.location.href = window.location.href.replace("/chi/", "/en/");
}
注:在JS中,當您修改window.location.href
時,新URL將自動加載。
這裏的a working fiddle讓你玩。
很高興爲你效勞。如果你喜歡這裏的任何答案,你應該接受它。否則,請留下一些意見,讓我們知道缺少的是什麼。 – 2012-04-02 02:46:21