JavaScript面向物件選項卡
阿新 • • 發佈:2018-12-23
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <style> #div1 input {background:#CCC;} #div1 .active {background:yellow;} #div1 div {width:200px; height:200px; background:#CCC; display:none;} </style> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標題文件</title> <script> window.onload=function () { var oTab=new TabSwitch('div1'); }; function TabSwitch(id) { var oDiv=document.getElementById(id); this.aBtn=oDiv.getElementsByTagName('input'); this.aDiv=oDiv.getElementsByTagName('div'); var i=0; var _this=this; for(i=0;i<this.aBtn.length;i++) { this.aBtn[i].index=i; this.aBtn[i].onclick=function () { _this.tab(this); }; } } TabSwitch.prototype.tab=function (oBtn) { for(i=0;i<this.aBtn.length;i++) { this.aBtn[i].className=''; this.aDiv[i].style.display='none'; } oBtn.className='active'; this.aDiv[oBtn.index].style.display='block'; }; </script> </head> <body> <div id="div1"> <input class="active" type="button" value="教育" /> <input type="button" value="財經" /> <input type="button" value="aaa" /> <div style="display:block;">1asdfasdfds</div> <div>2xzcvxzcv</div> <div>5332342345</div> </div> </body> </html>