我想使用jquery在语言部分之后附加当前路径

时间:2018-05-31 08:49:00

标签: javascript jquery

我想更改点击语言名称的网址。只需在语言部分后附加当前路径即可。例如:将网址https://example.com/de_DE/about-us/更改为https://example.com/us_EN/about-us/,反之亦然。 我试过这段代码,但没有正常工作

<script type="text/javascript">
$("#eng_btn").click(function()
{
     var bn_pathname = window.location.href;
     var en_pathname = bn_pathname.replace("/de_DE/","/en_Us/"); 
    window.location.href = en_pathname;

});

$("#ger_btn").click(function()
{
     var bn_pathname = window.location.href;
     var en_pathname = bn_pathname.replace("/en_Us/","/de_DE/"); 
     window.location.href = en_pathname;

});
</script>
<div class="sidebar_lang">
	<span>Language:</span>
	<div class="lang_option">
        <a href="javascript:void(0)" id='ger_btn' class="lang_txt">DE</a>
	<a href="javascript:void(0)" id="eng_btn" class="lang_txt">EN</a>
	</div>
</div>

2 个答案:

答案 0 :(得分:0)

它对我来说很好

&#13;
&#13;
var url = 'https://beta.yourtaxi.ch/de_DE/about-us/'

$("#eng_btn").click(function()
{
     var en_pathname = url.replace("/de_DE/","/en_Us/"); 
     console.log( en_pathname);

});

$("#ger_btn").click(function()
{
     var en_pathname = url.replace("/en_Us/","/de_DE/"); 
     console.log( en_pathname);

});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="sidebar_lang">
	<span>Language:</span>
	<div class="lang_option">
        <a href="javascript:void(0)" id='ger_btn' class="lang_txt">DE</a>
	<a href="javascript:void(0)" id="eng_btn" class="lang_txt">EN</a>
	</div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

MS SQL Server 2017