如何使用 window.location.href.replace 在 href url 中切换语言



我有一个链接,用于在我的网站中切换语言。当用户单击链接时,我希望它将用户定向到位于/ch/文件夹中的中文版本的页面。

例如,当用户在 mysite.com/account/en/index.html 上时,当单击链接时,应将用户重定向到 mysite.com/account/ch/index.html

我尝试了下面的代码,但它不起作用。有人知道更好的代码吗?

<li class="lang"><a id="langswitch" href="javascript: window.location.href.replace('/en/', '/ch/');" ><img src="/images/account/buttons/chinese.jpg" alt="chinese language" align="top" /></a></li>

我相信你缺少的只是

window.location = window.location.href.replace('/en/', '/ch/');

为什么你不能使用

<li class="lang"><a id="langswitch" href="mysite.com/account/cn/index.html" ><img src="/images/account/buttons/chinese.jpg" alt="chinese language" align="top" /></a></li>

最新更新