原生javascript選項卡菜單代碼

來源:互聯網
上載者:User
提示:您可以先修改部分代碼再運行

<!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>
提示:您可以先修改部分代碼再運行

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.