function $(id) { return typeof id === 'string' ? document.getElementById(id) : id; } window.onload = function () { var titles = $('main-til').getElementsByTagName('li'); var divs = $('main-con').getElementsByTagName('div'); if (titles.length != divs.length) return; for (var i = 0; i < titles.length; i++) { titles[i].id = i; //索引 titles[i].onmouseover = function () { for (var j = 0; j < titles.length; j++) { titles[j].className = ''; divs[j].style.display = 'none'; } this.className = 'select'; divs[this.id].style.display = 'block'; } } }
这段JavaScript代码是通过
var divs = $('main-con').getElementsByTagName('div'); divs[this.id].style.display = 'block';
来实现切换的,可如果title对应的内容栏里面又嵌套了其他几个div呢? 有没有什么视频、博客、课程来解决这个问题的?