var submenus = document.getElementsByClassName("submenu");
for (var i = 0; i < submenus.length; i++) {
submenus[i].onclick = function() {
toggle(submenus[i].nextSibling);
return false;
}
}
function toggle(el) {
if (el.style.display == 'block') {
el.style.display = 'none';
} else {
el.style.display = 'block';
}
}
导致错误:TypeError: submenus[i] is undefined
我假设submenus[i]
不在函数的范围内。如何点击元素以便切换它的下一个兄弟?