无法使以下代码正常工作。直接调用 swapLevel() 可以正常工作,但是 addEventListener() 似乎没有做任何事情?
JavaScript:
<script>
function load() {
var arr = document.getElementById('menu').getElementsByClassName('level');
for (var i=0; i<arr.length; i++) {
arr[i].addEventListener('click', function(){swapLevel(i);}, false);
}
// automatically open the first level
swapLevel(0);
}
document.addEventListener('DOMContentLoaded', load, false);
</script>
HTML:
<div id="menu">
<a href="#" class="level">Level One</a>
<span class="hidden" id="0">
<p>Some options here</p>
</span>
<a href="#" class="level">Level Two</a>
<span class="hidden" id="1">
<p>More options</p>
</span>
<a href="#" class="level">Level Three</a>
<span class="hidden" id="2">
<p>Even more options</p>
</span>
</div>