Heim > Fragen und Antworten > Hauptteil
P粉5641921312023-08-16 12:53:06
我不确定我是否正确理解了问题,但据我所知,有两种可能的解决方案,它们都包括使用'my_own_css_class'
<div class="hoverable-element">元素1</div> <div class="hoverable-element">元素2</div> <div class="hoverable-element">元素3</div>
.hoverable-element { /* 无 */ } .hoverable-element:hover { /* 重新着色 */ }
<div class="hoverable-element-js">元素1</div> <div class="hoverable-element-js">元素2</div> <div class="hoverable-element-js">元素3</div>
.hoverable-element-js { /* 无 */ } .hover-effect-js { /* 重新着色 */ }
const elements = document.querySelectorAll('.hoverable-element-js'); elements.forEach(element => { element.addEventListener('mouseover', () => { element.classList.add('hover-effect-js'); }); element.addEventListener('mouseout', () => { element.classList.remove('hover-effect-js'); }); });