Rumah > Soal Jawab > teks badan
P粉5641921312023-08-16 12:53:06
Saya tidak pasti sama ada saya memahami soalan dengan betul tetapi setahu saya terdapat dua penyelesaian yang mungkin dan kedua-duanya melibatkan penggunaan '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'); }); });