我正在尝试使用对象的 onclick 通知程序更改 div 的类。代码看起来应该可以工作,但是当我使用 Firebug 进行故障排除时,似乎我的 for 循环(甚至 Firebug 显示其中只有 1 个元素)执行了不止一次,并且第二次抛出错误。这是Javascript:
function handleElements(elementid,containerid) {
// Get array of all 'visible' elements in the container
var elements = document.getElementById(containerid).getElementsByClassName('visible');
// Iterate over that array and make them all 'hidden'
for (var i in elements) {
var object = elements[i].id;
document.getElementById(object).className='hidden';
}
// Get the 'clicked' tab and set it to 'visible'
var clicked = document.getElementById(elementid);
clicked.className='visible';
}
这是HTML:
<div id="wrapper">
<div id="leftpanel">
<div id="navcontainer">
<ul id="navlist">
<li><a href="javascript:;" onClick="handleElements('Modules','rightpanel')">Modules</a></li>
<li><a href="javascript:;" onClick="handleElements('DataViewer','rightpanel')">Data Viewer</a><li>
<li><a href="javascript:;" onClick="handleElements('Alarms','rightpanel')">Alarms</a><li>
<li><a href="javascript:;" onClick="handleElements('Logging','rightpanel')">Logging</a><li>
<li><a href="javascript:;" onClick="handleElements('Outputs','rightpanel')">Output Control</a><li>
</ul>
</div>
</div>
<div id="rightpanel">
<div id="Modules" class="visible">
<h2>Module Information Here</h2>
</div>
<div id="DataViewer" class="hidden">
<h2>Data Viewer Here</h2>
</div>
<div id="Alarms" class="hidden">
<h2>Alarm Page Here</h2>
</div>
<div id="Logging" class="hidden">
<h2>Logging Setup Here</h2>
</div>
<div id="Outputs" class="hidden">
<h2>Output Control Here</h2>
</div>
</div>