tab标签页切换

第一种:

<div>
	<ul id="tab-title">
		<li class="current">温度报表</li>
		<li>湿度报表</li>
	</ul>
	<div id="tab-cont">
 		<div style="display:block;">first</div>
 		<div>second</div>
 	</div>
</div>
<script>
	$('#tab-title li').click(function(){
		$(this).addClass('current').siblings().removeClass('current').parent().next().children().eq($(this).index()).show().siblings().hide();
	})
</script>

第二种:

  * {
    margin: 0;
    padding: 0;
    list-style: none;
    font-size: 12px;
  }
  .notice {
    width: 298px;
    height: 98px;
    margin: 10px;
    border: 1px solid #7c7c7c;
    overflow: hidden;
  }
  .notice-tit {
    height: 27px;
    background-color: #eaeaea;
    position: relative;
  }
  .notice-tit ul {
    position: absolute;
    width: 300px;
    left: -1px;
  }
  .notice-tit ul li {
    float: left;
    width: 58px;
    height: 26px;
    line-height: 26px;
    text-align: center;
    border-bottom: 1px solid #7c7c7c;
    padding: 0 1px;
  }
  .notice-tit ul a {
    text-decoration: none;
    display: block;
  }
  .notice-tit ul .select {
    background-color: white;
    border-right: 1px solid #7c7c7c;
    border-left: 1px solid #7c7c7c;
    border-bottom: 1px solid white;
    padding: 0;
  }
<div class="notice">
	<div class="notice-tit" id="notice-tit">
       <ul>
         <li class="select"><a href="#">公告</a></li>
         <li><a href="#">规则</a></li>
         <li><a href="#">论坛</a></li>
         <li><a href="#">安全</a></li>
         <li><a href="#">公益</a></li>
       </ul>
 	</div>
   	<div class="notice-con" id="notice-con">
       	<div style="display: block">我是内容1</div>
       	<div style="display: none">我是内容2</div>
       	<div style="display: none">我是内容3</div>
       	<div style="display: none">我是内容4</div>
       	<div style="display: none">我是内容5</div>
  	</div>
</div>
<script>
 	//获取id封装成一个函数$()方便调用
 	function $(id) {
     	//如果传入的参数类型为字符串则获取当前ID元素,否则返回id
        return typeof id === "string" ? document.getElementById(id) : id;
	}
	//window.onload表示当文档加载完毕时执行函数
	window.onload = function() {
		//获取#notice-tit下面的全部li元素
        var titles = $("notice-tit").getElementsByTagName("li");
        //获取#notice-con下面的全部div元素
        var divs = $("notice-con").getElementsByTagName("div");

        //遍历所有li标签,给每个li加上id和值,并且绑定事件
        for (var i = 0; i < titles.length; i++) {
        	//给每个li加上id和值
          	titles[i].id = i;
          	//给每个li绑定事件
          	titles[i].onmouseover = function() {
            	//悬浮后首先应该初始化每个li和div上的类和display
            	for (var j = 0; j < titles.length; j++) {
              		titles[j].className = "";
              		divs[j].style.display = "none";
           	 	}
            	//给当前悬浮元素添加属性
            	titles[this.id].className = "select";
            	divs[this.id].style.display = "block";
          	};
        }
	};
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值