博主信息
博文 40
粉丝 2
评论 1
访问量 46465
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
21.js Tag切换-2019年02月01号
万物皆对象
原创
1086人浏览过

实例

<!DOCTYPE html>
<html>
<head>
	<title>Tag切换</title>
	<style type="text/css">
		*{margin: 0 auto;padding: 0;}
		.tag{
			width: 500px;
			height: 300px;
			background: red;
		}
		.tag .nav{
			width: 500px;
			height: 50px;
		}
		.tag .nav span{
			width: 100px;
			height: 50px;
			float: left;
			line-height: 50px;
			text-align: center;
			font-size: 12px;
			background: pink;
			cursor: pointer;
			border-bottom: 1px solid #ccc;
		}
		.tag .nav span:hover{
			font-size: 20px;
			color: skyblue;
		}
		.tag .content{
			width: 100%;
			height: 250px;
			background: #fff;
		}
		.tag .content div{
			width: 500px;
			height: 250px;
			line-height: 250px;
			text-align: center;
			font-size: 40px;
			font-weight: 300;
			display: none;
		}
	</style>
	<script type="text/javascript">
		window.onload = function(){
			var spanList = document.getElementsByTagName('span');
			spanList[0].style.background = 'deeppink';
			document.getElementById('content').getElementsByTagName('div')[0].style.display = 'block';
		}

		function changeTag(obj){
			var num = obj.getAttribute('name');
			var spanLen = document.getElementsByTagName('span');
			console.log(spanLen);
			for(var i=0;i<spanLen.length;i++){
				if(i==num){
					spanLen[i].style.background = 'deeppink';
					spanLen[i].style.fontSize = '20px';
					spanLen[i].style.color = 'skyblue';
					document.getElementById('content').getElementsByTagName('div')[i].style.display = 'block';
				}else{
					spanLen[i].style.background = 'pink';
					spanLen[i].style.fontSize = '12px';
					spanLen[i].style.color = '#000';
					document.getElementById('content').getElementsByTagName('div')[i].style.display = 'none';
				}
			}
		}
	</script>
</head>
<body>
	<div class="tag">
		<div class="nav">
			<span name="0" onclick="changeTag(this)">首页</span>
			<span name="1" onclick="changeTag(this)">电影</span>
			<span name="2" onclick="changeTag(this)">电视</span>
			<span name="3" onclick="changeTag(this)">动漫</span>
			<span name="4" onclick="changeTag(this)">综艺</span>
		</div>
		<div class="content" id="content">
			<div style="background:pink;">1</div>
			<div style="background:lightblue;">2</div>
			<div style="background:lightgray;">3</div>
			<div style="background:lightgreen;">4</div>
			<div style="background:skyblue;">5</div>
		</div>
	</div>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

6666.jpg

批改状态:未批改

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学