提示:您可以先修改部分代碼再運行
<!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><title>簡潔實用的原生javascript選項卡菜單</title><meta http-equiv="content-type" content="text/html;charset=gb2312"><!--把下面代碼加到<head>與</head>之間--><style type="text/css">ul,li{margin:0;padding:0;overflow:hidden;list-style:none;font-family:"Lucida Console",Monaco,monospace;}#tab{width:300px;height:25px;border:1px solid #ddd;}#tab li{width:75px;height:25px;line-height:26px;text-align:center;float:left;cursor:pointer;}#tab li.curr{background:#eee;}#con{width:300px;border:1px solid #ddd;margin-top:-1px;}#con li{display:none;width:280px;height:100px;padding:10px;}</style></head><body><!--把下面代碼加到<body>與</body>之間--><div id="tab"><ul><li>1</li><li>2</li><li>3</li><li>4</li></ul></div><div id="con"><ul><li>a</li><li>b</li><li>c</li><li>d</li></ul></div><script type="text/javascript">/*選項卡函數:cmd:點擊元素集合con:顯示容器集合evt:觸發事件css:為當前點擊元素的樣式名稱index:為預設顯示第幾項的索引值email: kingark@163.com*/(function(t){window[t] = function(cmd, con, evt, css, index){//預設觸發事件var evt = evt || 'mouseover',//預設樣式名css = css || 'curr',index = index || 0;//初始化顯示項目show(index);//為點擊元素繫結事件for(var i = 0, l = cmd.length; i < l; i ++){//為準確獲得i的值用閉包傳值(function(n){cmd[n]['on'+ evt] = function(){//切換到索引為i的選項show(n);}})(i);};//切換顯示function show(i){cmd[index].className = cmd[index].className.replace(css, '');con[index].style.display = 'none';index = i;cmd[index].className += css;con[index].style.display = 'block';}}//指定選項卡函數的名稱})('Tab');function tag(i, t){return document.getElementById(i).getElementsByTagName(t);};//調用選項卡函數Tab(tag('tab','li'), tag('con','li'), 'click', '', 1);</script></body></html></td> </tr></table>
提示:您可以先修改部分代碼再運行