[英]Add a class to element with matching data attribute value
I want to add a class to an element that shares the same data attribute value using vanilla JS. 我想使用香草JS向共享相同数据属性值的元素添加一个类。 The class is added on
mouseenter
. 该类添加在
mouseenter
。
My current setup only applies the class on hover to the first element and ignores the rest. 我当前的设置仅将鼠标悬停时的类应用于第一个元素,而忽略其余元素。
let section = document.querySelector('.section'); let links = document.querySelectorAll('.links a'); let triggerVal; let linkedVal; links.forEach(function(link, index) { link.addEventListener('mouseenter', function() { triggerVal = this.dataset.triggerValue; linkedVal = section.dataset.linkedValue; if (linkedVal === triggerVal) { section.classList.add('is-active'); } else { section.classList.remove('is-active'); } }); });
<ul class="links"> <li> <a data-trigger-value="red" href="#">Red</a> </li> <li> <a data-trigger-value="yellow" href="#">Yellow</a> </li> <li> <a data-trigger-value="blue" href="#">Blue</a> </li> </ul> <div class="wrapper"> <div class="section" data-linked-value="red"> <h2>Red</h2> </div> <div class="section" data-linked-value="yellow"> <h2>Yellow</h2> </div> <div class="section" data-linked-value="blue"> <h2>Blue</h2> </div> </div>
Here's a Codepen: https://codepen.io/abbasarezoo/pen/7378e190ed6ad117faca968b634520b0 这是一个Codepen: https ://codepen.io/abbasarezoo/pen/7378e190ed6ad117faca968b634520b0
I've got a feeling it's to do with the .section
element but I've tried a few things and nothing seems to give me what I need. 我觉得这与
.section
元素有关,但是我尝试了几件事,但似乎没有任何东西.section
我的需求。
Any suggestions as to what I need to do to get the rest of the elements working? 关于我需要做什么才能使其余元素正常工作的任何建议?
You need to change two things: 您需要更改两件事:
First, get all sections: 首先,获取所有部分:
const section = document.querySelectorAll('.section');
Then, inside your handler, you need to iterate over the NodeList
returned by querySelectorAll()
: 然后,在处理程序内部,您需要遍历
querySelectorAll()
返回的NodeList
:
for (const section of sections) {
linkedVal = section.dataset.linkedValue;
if (linkedVal === triggerVal) {
section.classList.add('is-active');
} else {
section.classList.remove('is-active');
}
}
This is your new JS: 这是您的新JS:
const sections = document.querySelectorAll('.section');
const links = document.querySelectorAll('.links a');
let triggerVal;
let linkedVal;
links.forEach(function(link, index){
link.addEventListener('mouseenter', (e) => {
triggerVal = e.target.dataset.triggerValue;
for (const section of sections) {
linkedVal = section.dataset.linkedValue;
if (linkedVal === triggerVal) {
section.classList.add('is-active');
} else {
section.classList.remove('is-active');
}
}
});
});
You need to use document.querySelectorAll
for sections
and then forEach
. 您需要将
document.querySelectorAll
用于sections
,然后用于forEach
。 And use toggle
instead of add/remove
for this case. 并在这种情况下使用
toggle
而不是add/remove
。 https://developer.mozilla.org/en-US/docs/Web/API/Element/classList https://developer.mozilla.org/en-US/docs/Web/API/Element/classList
let sections = document.querySelectorAll('.section');
let links = document.querySelectorAll('.links a');
let triggerVal;
let linkedVal;
links.forEach(function(link, index) {
link.addEventListener('mouseenter', function() {
triggerVal = this.dataset.triggerValue;
sections.forEach(
section => section.classList.toggle(
'is-active',
section.dataset.linkedValue === triggerValue
)
);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.