Javascript 如何使用window.location.href.replace切换href url中的语言?

Javascript 如何使用window.location.href.replace切换href url中的语言?,javascript,jquery,html,Javascript,Jquery,Html,我有一个用于在我的站点中切换语言的链接。当用户单击链接时,我希望它将用户指向位于/ch/文件夹内的页面的中文版本 例如,当用户在mysite.com/account/en/index.html上时,当单击链接时,用户应重定向到mysite.com/account/ch/index.html 我尝试了下面的代码,但它不工作。有人知道更好的代码吗 <li class="lang"><a id="langswitch" href="javascript: window.locatio

我有一个用于在我的站点中切换语言的链接。当用户单击链接时,我希望它将用户指向位于/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/”)

    我相信你所缺少的只是

    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>
    
  • 为什么不能使用

    <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>
    

  • 我认为他们不使用常规链接的原因是,锚定将切换到中文网站上的当前页面,而不是总是转到index.html。那么,使用锚定标记就没有意义了>将onclick事件添加到#langswitch我认为他们不使用常规链接的原因是,主播可以切换到中文网站上的当前页面,而不是总是转到index.html。那么,使用主播标记就没有意义了>将onclick事件添加到#langswitch
    ch
    通常指瑞士;使用
    zh
    引用中文!参见:
    ch
    通常指瑞士;使用
    zh
    引用中文!请看:这对我有效,我的最终代码是:
  • 这对我有效,我的最终代码是: