我如何根据所选语言订购品牌div?

问题描述 投票:0回答:2

我需要实现以下目标:在页面顶部更改语言时,品牌div(带有纸飞机的条带)会更改顺序以显示首先使用该语言的网站。这将在wordpress网站上并使用翻译插件。我有点不确定从哪里开始,我对onclick事件有一点了解,但我的知识非常有限。目前的开发网站可以在这里找到:http://atsreisen.eighty3.co.uk/de/

javascript jquery html css wordpress
2个回答
0
投票

如果我完全理解,你需要将顶部的语言附加到品牌div。您可以通过在html和ids中将data-lang属性添加到链接中来实现。 (data-something)是一种创建自定义属性以在jQuery中使用的方法。

<a class="lang" href="#0" id="german">German</a>
<a class="lang" href="#0" id="english">English</a>
<div id="container">
  <div data-lang="german">This is a brand div</div>
  <div data-lang="english">This is another brand div</div>
  <div data-lang="english">This is an English brand div</div>
</div>

然后在jQuery中你可以这样做:

//variables (caching)
var langLink = $('.lang');
var langId;
var dataLang;

langLink.on('click', function(event) {
  // this part to prevent the page from refreshing when the link is clicked
  event.preventDefault();
  // get the clicked links Ids, hence $(this)
  langId = $(this).attr('id');
  // go up to a parent the links and divs share here it's the body
  // then finding the divs that has data-lang matching the links id
  dataLang = $(this).parent('body').find('div[data-lang="' + langId + '"]');
  // a temporary storage to store all the divs that match the language clicked
  var temp = [];
  // pushing the found divs to the temp storage
  temp.push(dataLang);
  // removing them from the dom
  dataLang.remove();
  // and finally looping through the temp storage array and prepending it to the container
  for (var i = 0; i < temp.length; i++) {
    $('#container').prepend(temp[i]);
  }
});

我就是这样做的,但我确信还有其他方法可以做到这一点。如果你想看到它在行动尝试这个JSFiddle


0
投票

如果您使用的是wordpress,则可以通过多种方式实现此目的。最简单的方法是修改wordpress主题标题(理想情况下通过子主题),并添加“if条件”来检测当前的wordpress语言并相应地向主体添加一个类;你可以通过使用wordpress get_locale()函数来做到这一点。然后你可能需要一个小的javascript,或理想的jQuery,来获得品牌div并重新排序他们的身体类。

© www.soinside.com 2019 - 2024. All rights reserved.