本文實例為大家分享了用JS制作簡易的可切換的年歷,類似於選項卡,供大家參考,具體內容如下
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> #box{ background-color: green; border-radius: 20px; padding: 10px; height: 800px; width: 450px; margin: 20px auto; } ul,li{ margin: 0; padding: 0; list-style-type: none; } ul{ overflow: hidden; } li{ background-color: yellow; height: 120px; width:120px; float: left; margin: 10px; text-align: center; color:blueviolet; } #bottom{ margin: 10px; } .active{ background-color: crimson; } </style> </head> <body> <div id="box"> <ul> <li class="active"><h2>一月</h2><p>January </p></li> <li><h2>二月</h2><p>February </p></li> <li><h2>三月</h2><p>March </p></li> <li><h2>四月</h2><p> April </p></li> <li><h2>五月</h2><p> May </p></li> <li><h2>六月</h2><p>June</p></li> <li><h2>七月</h2><p>July </p></li> <li><h2>八月</h2><p>August</p></li> <li><h2>九月</h2><p>September </p></li> <li><h2>十月</h2><p> October </p></li> <li><h2>十一月</h2><p> November </p></li> <li><h2>十二月</h2><p>December</p></li> </ul> <div id="bottom"> <h2>一月</h2> <p>天氣很好,去哪裡玩呢???</p> </div> </div> <script type="text/javascript"> var box=document.getElementById("box"); var bottom=document.getElementById("bottom"); var li=box.getElementsByTagName("li"); for (var i=0;i<li.length;i++) { li[i].index=i; var arr=["一月吃牛排","二月吃意面","三月看帥哥","四月看星星" ,"五月吃豬蹄","六月吃西瓜","七月吃雪糕","八月吹空調","九月撿樹葉","十月吃烤雞", "十一月吃麻辣燙","十二月回家過年"]; li[i].onmouseover=function(){//事件綁定 for (var i=0;i<li.length;i++) { //清除所有的li的className li[i].className=" "; } this.className="active";//給當前的li添加className bottom.innerHTML='<h2>'+(this.index +1)+'月活動 </h2><p>'+arr[this.index]+'</p>'; } } </script> </body> </html>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持。