简单tab

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Customized Tab</title>
<style type="text/css">
	.tab{
		margin-top:2px;
		color: #5A5A5A;
		font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;
		font-size: 12px;
		font-weight: bold;
		background-color:#E6E6E6;
		display:inline-block;
		padding:5px;
		width:100px;
		text-align:center;
		overflow-x:visible;
		border-top:1px solid lightGrey;
		border-left:1px solid lightGrey;
		border-right:1px solid lightGrey;
		border-top-left-radius:8px;
		border-top-right-radius:8px;
	}
	
	.tab:hover{
		border-top:1px solid #999;
		border-left:1px solid #999;
		border-right:1px solid #999;
 		background-color:#DADADA;
		cursor:pointer;
	}
	
	#selected{
 		background-color:white;
		border-bottom:1px solid white; 
 		margin-bottom:-1px;
	}
	
	.tabs{
 		border: 1px solid #AAA;
		background: #CCC;
		border-top-left-radius: 4px;
		border-top-right-radius: 4px;
		border-bottom-right-radius: 4px;
		border-bottom-right-radius: 4px;
		margin-left:50px;
		margin-top:20px;
		width:500px;
	}
	
	.tabsContainer{
		margin-left:50px;
	}
</style>

<script type="text/javascript">
window.οnlοad=function(){
	var divs=document.getElementsByTagName("div");
	
	var tabs=[];
	var tabsContainer=null;
	
	for(var i=0;i<divs.length;i++){
		var div=divs[i];
		if(div.className=="tab"){
			tabs.push(div);
		}
		if(div.getAttribute("name")=="tabsContainer"){
			tabsContainer=div;
		}
	}
	
	for(var i=0;i<tabs.length;i++){
		var tab=tabs[i];
		tab.οnclick=function(){
			var href=this.getAttribute("href");
			
			for(var k=0;k<tabs.length;k++){
				tabs[k].removeAttribute("id");
			}
			
			this.setAttribute("id","selected");
			
			for(var n=0;n<tabsContainer.childNodes.length;n++){
				var childNode=tabsContainer.childNodes[n];
				
				if(childNode.nodeType!=1){
					continue;
				}
				if(childNode.getAttribute("name")==href){
					childNode.style.display="block";
				}else{
					childNode.style.display="none";
				}
			}
			
		};
	}
};
</script>
</head>
<body>
	<div class="tabs">
		<div class="tab" href="tab1" title="tab1" id="selected">tab1</div>
		<div class="tab" href="tab2" title="tab2">tab2</div>
		<div class="tab" href="tab3" title="tab3">tab3</div>
	</div>
	
	<div name="tabsContainer" class="tabsContainer">
		<div name="tab1" >This is content of tab1</div>
		<div name="tab2" style="display:none;">This is content of tab2</div>
		<div name="tab3" style="display:none;">This is content of tab3</div>
	</div>
</body>
</html>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HTML代码: ``` <div class="tab"> <button class="tablinks" onclick="openTab(event, 'tab1')">Tab 1</button> <button class="tablinks" onclick="openTab(event, 'tab2')">Tab 2</button> <button class="tablinks" onclick="openTab(event, 'tab3')">Tab 3</button> </div> <div id="tab1" class="tabcontent"> <h3>Tab 1 Content</h3> <p>This is the content of Tab 1.</p> </div> <div id="tab2" class="tabcontent"> <h3>Tab 2 Content</h3> <p>This is the content of Tab 2.</p> </div> <div id="tab3" class="tabcontent"> <h3>Tab 3 Content</h3> <p>This is the content of Tab 3.</p> </div> ``` CSS代码: ``` /* 隐藏所有选项卡内容 */ .tabcontent { display: none; } /* 样式化选项卡按钮 */ .tab { overflow: hidden; border: 1px solid #ccc; background-color: #f1f1f1; } .tab button { background-color: inherit; float: left; border: none; outline: none; cursor: pointer; padding: 14px 16px; transition: 0.3s; } .tab button:hover { background-color: #ddd; } .tab button.active { background-color: #ccc; } /* 样式化选项卡内容 */ .tabcontent { padding: 6px 12px; border: 1px solid #ccc; border-top: none; } ``` JavaScript代码: ``` function openTab(evt, tabName) { // 隐藏所有选项卡内容 var i, tabcontent, tablinks; tabcontent = document.getElementsByClassName("tabcontent"); for (i = 0; i < tabcontent.length; i++) { tabcontent[i].style.display = "none"; } // 取消所有选项卡按钮的激活状态 tablinks = document.getElementsByClassName("tablinks"); for (i = 0; i < tablinks.length; i++) { tablinks[i].className = tablinks[i].className.replace(" active", ""); } // 显示当前选项卡内容并激活按钮 document.getElementById(tabName).style.display = "block"; evt.currentTarget.className += " active"; } ``` 以上代码实现了一个简单的选项卡切换效果。点击每个按钮时,会显示对应的选项卡内容,并且激活该按钮。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值