我終於弄清楚是什麼讓我稱之爲「文件夾」的元素打開和關閉。問題是當我通過validator運行HTML文檔時,它說屬性<isfolder="true">
此時不允許在div
元素上。JavaScript驗證錯誤
<p></p>
<div class="folderOpenClose" onclick="toggleAllFolders();">Open/Close Folders</div>
<p></p>
<div class="folderlabel" onclick="togglefolder('folder0');">Dice Roller</div>
<div id="folder0" class="folder" isfolder="true" style="display: none;">
</div>
<p></p>
<div class="folderlabel" onclick="togglefolder('folder1');">Slide Show</div>
<div id="folder1" class="folder" isfolder="true" style="display: none;">
</div>
<p></p>
<div class="folderlabel" onclick="togglefolder('folder2');">Carousel</div>
<div id="folder2" class="folder" isfolder="true" style="display: none;">
</div>
<p></p>
<div class="folderlabel" onclick="togglefolder('folder3');">Menu Bar</div>
<div id="folder3" class="folder" isfolder="true" style="display: none;">
</div>
<p></p>
我去除的問題是它什麼操作打開/關閉所有,當我試圖把代碼中的形式,打開/關閉文件夾不起作用。
var lastChecked = null;
function togglefolder(id){
var ele = object(id);
if(ele.style.display == 'none') ele.style.display='block';
else ele.style.display = 'none';
}
function setAllFolders(how){
var way = ""+how;
if(way == "") way = "none";
if(way == "closed") way = "none";
if(way == "open") way = "block";
var divs = window.document.getElementsByTagName('div');
for(var i=0; i < divs.length; i++){
if(divs[i].getAttribute('data-isfolder')&&divs[i].getAttribute('data-isfolder')=='true')
divs[i].style.display = way;
var thisClass = ""+divs[i].getAttribute('class');
if(thisClass=='folderlabel'){
var thelabel = ""+divs[i].innerHTML;
if(thelabel.indexOf('open/close all folders') != -1)
divs[i].setAttribute('class','folderlabelopen');
}
}
last_toggle=way;
}
請問您可以將您的代碼縮減到最低限度的數量嗎?看看[mcve]。 –