function?$(id){
return?typeof?id?===?"string"?document.getElementById(id):id;
}
var?timer?=?null;
var?index?=?0;
window.onload?=?tab;
//這兩行放外面會(huì)報(bào)錯(cuò),
//Uncaught?TypeError:?Cannot?read?property?'getElementsByTagName'?of?null
tab.js:13?
//Uncaught?TypeError:?Cannot?read?property?'length'?of?undefined
var?titles?=?$('notice-title').getElementsByTagName('li');
var?divs?=?$('notice-con').getElementsByTagName('div');
function?tab(){
//手動(dòng)切換
for?(var?i?=?0;?i?<?titles.length;?i++)?{
titles[i].id?=?i;
titles[i].onmouseover?=?function(){
clearInterval(timer);
//?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";
changeIndex(this.id);
}
titles[i].onmouseout?=?function(){
clearInterval(timer);
timer?=?setInterval(changeTab,2000);
}
};
//自動(dòng)切換頁切
timer?=?setInterval(changeTab,2000);
//?定義好的changeTab函數(shù)
function?changeTab(){
//?var?titles?=?$('notice-title').getElementsByTagName('li');
//?var?divs?=?$('notice-con').getElementsByTagName('div');
index++;
if(index>=titles.length){
index?=?0;
}
//?其他樣式去掉高亮
changeIndex(index);
}
function?changeIndex(curIndex){
for?(var?i?=?0;?i?<?titles.length;?i++)?{
titles[i].className?=?"";
divs[i].style.display?=?"none";
};
//高亮顯示標(biāo)簽
titles[curIndex].className?=?"select";
divs[curIndex].style.display?=?"block";
}
}
2016-04-10
頁面沒加載完是找不到dom元素的,window.onload是等待頁面dom和其他資源都加載后再執(zhí)行dom查找。