[英]Select a DOM element based on it's data attributes
I have an HTML structure such as this: 我有这样的HTML结构:
<span class="portfolio-custom-content" data-name="alexa" data-lang="hu">Hungarian text</span>
<span class="portfolio-custom-content" data-name="alexa" data-lang="en">English text</span>
<span class="portfolio-custom-content" data-name="peter" data-lang="hu">Hungarian text</span>
<span class="portfolio-custom-content" data-name="peter" data-lang="en">English text</span>
<span class="portfolio-custom-content" data-name="james" data-lang="hu">Hungarian text</span>
<span class="portfolio-custom-content" data-name="james" data-lang="en">English text</span>
I would like to select the text according to an id and the current language (WPML). 我想根据ID和当前语言(WPML)选择文本。
Here's the javascript code I'm using: 这是我正在使用的javascript代码:
window.onhashchange = function () {
var url = window.location.pathname,
idk = [];
// IDs for the current portfolio
idk = {
1: 'alexa',
2: 'peter',
3: 'james'
}
// Checking the current language of the website. If it's in english, german or italian, we need the english text, else we need the hungarian text.
if (url.indexOf('/en/') > -1 || url.indexOf('/de/') > -1 || url.indexOf('/it/') > -1) {
// not hungarian language
var id = parseInt(window.location.hash.split("#")[1]),
selector = '.portfolio-custom-content[data-name="' + idk[id] + '"]';
if (!isNaN(id)) {
}
} else {
// hungarian language
}
}
As you can see, I'm able to select the correct span by it's id, but how do I have to modify the selector, so that I can select the name AND the language? 如您所见,我可以通过其ID选择正确的跨度,但是如何修改选择器,以便可以选择名称和语言?
只需对属性选择加倍:
selector = '.portfolio-custom-content[data-name="' + idk[id] + '"][data-lang="' + langVariable + '"]';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.