js实现TAB切换对应不同颜色的代码

 更新时间:2015年08月31日 14:50:10   作者:企鹅  
这篇文章主要介绍了js实现TAB切换对应不同颜色的代码,涉及javascript页面元素的遍历及样式的动态操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了js实现TAB切换对应不同颜色的代码。分享给大家供大家参考。具体如下:

这是一个个性化的TAB菜单,每个TAB卡片的背景颜色都不相同,鼠标点击上部的TAB,在主体内容区可看到颜色的变化。

运行效果截图如下:

在线演示地址如下:

http://demo.jb51.net/js/2015/js-cha-tab-color-nav-codes/

具体代码如下:

<!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>
<title>不同颜色选项卡</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<style type="text/css">
 * {
  margin: 0;
  padding: 0;
 }
 body {
  font: 12px/20px 'microsoft yahei', 'arial';
  word-break: break-all;
  word-wrap: break-word;
 }
 .clearfix:after {
  content: '.';
  display: block;
  clear: both;
  height: 0;
  visibility: hidden;
 }
 .clearfix {
  display: inline-block;
 }
 * html .clearfix {
  height: 1%;
 }
 .clearfix {
  display: block;
 }
 #wrap {
  width: 320px;
  margin: 2em auto;
 }
 .card_List {
  height: 30px;
  border-bottom: 1px solid #f00;
  position: relative;
 }
 .card_List li {
  float: left;
  width: 68px;
  text-align: center;
  height: 30px;
  line-height: 30px;
  margin: 0 5px;
  display: inline;
  border-top-left-radius: 6px;
  border-top-right-radius: 6px;
 }
 .card_List li.current {
  height: 34px;
  line-height: 34px;
  margin-top: -4px;
  border: 1px solid #F00;
  background: #FF9494;
  border-bottom: none;
  color: #fff;
 }
 #oLi li:nth-child(1){
  background: #FF9494;
 }
 #oLi li:nth-child(2){
  background: #8CFE8C;
 }
 #oLi li:nth-child(3){
  background: #6969FB;
 }
 #oLi li:nth-child(4){
  background: #FFE26F;
 }
 .card_content div {
  display: none;
  height: 100px;
  text-align: center;
  color: white;
 }
 .card_content div:first-child {
  background: #FF9494;
 }
</style>
<script type="text/javascript">
 window.onload = function () {
  var colorArr = {
   0:"#f00",
   1:"#0f0",
   2:"#00f",
   3:"#FC0"
  };
  var bgColorArr = {
   0:"#FF9494",
   1:"#8CFE8C",
   2:"#6969FB",
   3:"#FFE26F",
  }
  var oL = document.getElementById("oLi");
  var oLi = oL.getElementsByTagName("li");
  var oUl = document.getElementById("oUl").getElementsByTagName("div");
  for ( var i=0 ; i<oLi.length ; i++ ){
   oLi[i].index = i;
   oLi[i].onclick = function () {
    for ( var j = 0 ; j < oLi.length ; j++ ){
     oLi[j].className = "";
     oLi[j].style.border = "none";
    }
    this.className = "current";
    this.style.border = "1px solid " + colorArr[this.index];
    this.style.borderBottom = "none";
    oL.style.borderBottom = "1px solid " + colorArr[this.index];
    for ( var j=0 ; j < oUl.length ; j++ ){
     oUl[j].style.display = "none";
     oUl[this.index].style.display = "block";
     oUl[j].style.backgroundColor = bgColorArr[this.index];
    }
   };
  }
 };
</script>
</head>
<body>
 <div id="wrap">
  <ul id="oLi" class="card_List clearfix">
   <li class="current">1</li>
   <li>2</li>
   <li>3</li>
   <li>4</li>
  </ul>
  <div id="oUl" class="card_content">
   <div style="display:block;">
    11111111111111
   </div>
   <div>
    22222222222
   </div>
   <div>
    3333333333333
   </div>
   <div>
    44444444444444444
   </div>
  </div>
 </div>
</body>
</html>

希望本文所述对大家的javascript程序设计有所帮助。

相关文章

  • 如何在微信小程序中实现Mixins方案

    如何在微信小程序中实现Mixins方案

    这篇文章主要给大家介绍了关于如何在微信小程序中实现Mixins方案的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用微信小程序具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-06-06
  • 如何使用CocosCreator对象池

    如何使用CocosCreator对象池

    这篇文章主要介绍了CocosCreator对象池,对性能有研究的同学,要着重看一下
    2021-04-04
  • ie focus bug 解决方法

    ie focus bug 解决方法

    在IE中,新创建的input没有如预期的获得焦点。
    2009-09-09
  • JS实现常见的查找、排序、去重算法示例

    JS实现常见的查找、排序、去重算法示例

    这篇文章主要介绍了JS实现常见的查找、排序、去重算法,结合实例形式总结分析了JavaScript线性查找、二分查找、递归查找、数组去重、冒泡拍戏、快速排序实现技巧,需要的朋友可以参考下
    2018-05-05
  • ES6概念 ymbol.for()方法

    ES6概念 ymbol.for()方法

    这篇文章主要介绍了ES6概念 ymbol.for()方法,需要的朋友可以参考下
    2016-12-12
  • javascript实现简单的二级联动

    javascript实现简单的二级联动

    这篇文章主要介绍了javascript实现简单的二级联动,非常的实用,需要的朋友可以参考下
    2015-03-03
  • js数组中如何随机取出一个值

    js数组中如何随机取出一个值

    这篇文章主要介绍了js数组中如何随机取出一个值,需要的朋友可以参考下
    2014-06-06
  • JavaScript伪数组用法实例分析

    JavaScript伪数组用法实例分析

    这篇文章主要介绍了JavaScript伪数组用法,结合实例形式分析了伪数组的概念、功能、定义及简单使用方法,需要的朋友可以参考下
    2017-12-12
  • js和jq使用submit方法无法提交表单的快速解决方法

    js和jq使用submit方法无法提交表单的快速解决方法

    下面小编就为大家带来一篇js和jq使用submit方法无法提交表单的快速解决方法。小编觉得挺不错的,现在分享给大家,也给大家做个参考
    2016-05-05
  • JS打断点的六种常用姿势你用过几种

    JS打断点的六种常用姿势你用过几种

    JS断点调试,即是在浏览器开发者工具中为JS代码添加断点,让JS执行到某一特定位置停住,方便开发者对该处代码段的分析,这篇文章主要介绍了JS打断点的六种常用姿势的相关资料,需要的朋友可以参考下
    2025-04-04

最新评论