我需要实现以下目标:
在页面顶部更改语言时,品牌div(带纸飞机的条带)更改顺序以首先显示使用该语言的网站。这将在wordpress网站上并使用翻译插件。我不太确定从哪里开始,我对onclick事件有些了解,但是我的知识确实很有限。当前的开发站点可以在这里找到:http://atsreisen.eighty3.co.uk/de/
最佳答案
如果我理解得很好,则需要在品牌div的顶部附加语言。您可以通过在html和id中向链接添加data-lang属性来实现。 (数据事物)是一种创建自定义属性以在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