js鼠标经过就增加一个open 进行展开 遍历数组,为每个id对应的元素添加事件监听器
{% javascript %}const ids = ['Details-HeaderMenu-aaaaaa2','Details-HeaderMenu-aaaaaa6', 'Details-HeaderMenu-aaaaaa4','Details-HeaderSubMenu-1','Details-HeaderSubMenu-2','Details-HeaderSubMenu-3','Details-HeaderSubMenu-4','Details-HeaderSubMenu-5','Details-HeaderSubMenu-6','Details-HeaderSubMenu-7','Details-HeaderSubMenu-8']; // 遍历数组,为每个id对应的元素添加事件监听器
ids.forEach(id => { let detail = document.getElementById(id); // 使用变量id获取元素 if (detail) { // 检查元素是否存在 detail.addEventListener('mouseover', () => { // 绑定mouseover事件 if (!detail.hasAttribute('open')) { detail.setAttribute('open', ''); // 根据需要设置aria-expanded属性 // detail.setAttribute('aria-expanded', 'true'); } }); detail.addEventListener('mouseleave', () => { // 绑定mouseleave事件 if (detail.hasAttribute('open')) { detail.removeAttribute('open'); detail.setAttribute('aria-expanded', 'false'); } }); } else { console.warn(`No element found with id: ${id}`); // 如果找不到元素,输出警告信息 }
});