Home > Web Front-end > JS Tutorial > JavaScript implements tab label switching effect (code example)

JavaScript implements tab label switching effect (code example)

青灯夜游
Release: 2018-10-10 17:04:53
forward
2214 people have browsed it

This article will introduce how to use CSS to achieve the effect of QR code scanning. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

This is the html code

       <p id="main">
			<!--标题-->
			<ul id="tab">
				<li class="showed">tab1</li>
				<li>tab2</li>
				<li>tab3</li>
			</ul>
			<!--内容-->
			<p id="contents">
				<ul style="display:block;"><!--默认显示第一个-->
					<span>模块一</span>
				</ul>
				<ul>
					<span>模块二</span>
				</ul>
				<ul>
					<span>模块三</span>
				</ul>
			</p>
		</p>
Copy after login

CSS style

           li{
               list-style:none;
           }
			#main {
				width: 600px;
				margin: 200px auto;
			}
			
			#tab {
				overflow: hidden;
				background: #000;
				border: 1px solid #000;
			}
			
			#tab li {
				float: left;
				color: #fff;
				height: 30px;
				cursor: pointer;
				line-height: 30px;
				padding: 0 20px;
			}
			
			#tab li.showed {
				color: #000;
				background: #ddd;
			}
			
			#contents {
				border: 1px solid #000;
				border-top-width: 0;
			}
			
			#contents ul {
				line-height: 150px;
				display:none;
				margin: 0 30px;
				padding: 10px 0;
			}
Copy after login

The following is the JS

            $(function() {
				window.onload = function() {
					var lis = $(&#39;#tab li&#39;);
					var uls = $(&#39;#contents ul&#39;);

					lis.click(function() {
						var li_selected = $(this); //选中的li分类
						var num = li_selected.index(); //相对于同胞元素的位置
						lis.removeClass(); //清空liCSS属性
						li_selected.addClass(&#39;showed&#39;); //选中li添加属性
						uls.css(&#39;display&#39;, &#39;none&#39;); //隐藏所有ul标签
						uls.eq(num).css(&#39;display&#39;, &#39;block&#39;); //展示选中的li所对应的ul内容
					})
				}
			});
Copy after login

Rendering:

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study. For more related tutorials, please visit JavaScript Video Tutorial!

Related recommendations:

php public welfare training video tutorial

JavaScript graphic tutorial

JavaScriptOnline Manual

The above is the detailed content of JavaScript implements tab label switching effect (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template