【问题标题】:I want to change language url using jquery我想使用 jquery 更改语言 url
【发布时间】:2018-11-10 01:30:13
【问题描述】:

我想使用 jquery 更改语言 URL。我的网址是https://beta.yourtaxi.ch/de_DE/about-us/,只想更改它https://beta.yourtaxi.ch/en_Us/about-us/。我尝试了下面的代码,但没有工作。

$('a[rel="group"]').on('click', function(e) {
  e.preventDefault();
  location.href = $(this).data('wpurl');
  $(this).attr('href', wpurl);
});
<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="<?php echo site_url(); ?>/en_Us/" data-wpurl="<?php echo site_url(); ?>/de_DE/" class="lang_txt" rel="group">DE</a>
    <a href="<?php echo site_url(); ?>/de_DE/" data-wpurl="<?php echo site_url(); ?>/en_Us/" class="lang_txt" rel="group">EN</a>
  </div>
</div>

【问题讨论】:

  • location.href = '/en_Us/about-us'
  • 关于我们只是一个例子
  • 当我打开任何页面时,它只会更改 en_Us 和 de_DE
  • 我只想更改 en_Us 和 de_DE,反之亦然,不要在页面名称后更改
  • 您的实际问题是“如何将当前路径(在语言部分之后)附加到 URL”

标签: jquery href location-href


【解决方案1】:

您可以获取当前的路径,然后删除第一部分以附加到&lt;a&gt; 标记中的URL。

例如

const appendPath = location.pathname // current full path, eg "/de_DE/about-us/"
    .split('/') // split on "/" -> ["", "de_DE", "about-us", ""]
    .slice(2) // omit the first two parts -> ["about-us", ""]
    .join('/') // join in to a slash-delimited string -> "about-us/"

location.href = $(this).data('wpurl') + appendPath

【讨论】:

  • 是的,你是对的,但是我认为我在href中的html代码有问题
  • href里应该放什么?
  • @priyapatel 什么都没有。您正在离开该页面,所以没关系
猜你喜欢
  • 2021-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-18
  • 1970-01-01
  • 2016-01-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多