The example in this article describes the JS custom tab function and usage. Share it with everyone for your reference. The details are as follows:
Here I share a JS tab function with demonstration effect, tab function parameter calling instructions:
cmd: Click on the element collection
con: Display container collection
evt: trigger event
css: the style name of the currently clicked element
index: The index value of the default displayed item
At present, the style of the tab is relatively simple and rough. If you want to use it, you can beautify it yourself.
The screenshot of the running effect is as follows:
The online demo address is as follows:
http://demo.jb51.net/js/2015/js-zdy-tab-cha-fun-style-codes/
The specific code is as follows:
<!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> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Js选项卡</title> <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> <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 : [email]kingark@163.com[/email] */ (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>
I hope this article will be helpful to everyone’s JavaScript programming design.