方法一、主要使用了传递参数的思想,把循环变量不能使用转换了一下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.nav{
width: 100%;
height: 50px;
text-align: center;
}
.nav li{
display: inline-block;
padding-left: 20px;
padding-right: 20px;
list-style: none;
background: red;
color: #fff;
text-align: center;
height: 50px;
width: 100px;
line-height: 50px;
border-radius: 10px;
}
.container{
width: 100%;
height: 400px;
background: #cccccc;
border-radius: 20px;
text-align: center;
line-height: 400px;
}
.container div{
position: absolute;
/*background: #fff;*/
width: 80%;
left: 10%;
top: 15%;
height: 300px;
margin: 0 auto;
border-radius: 20px;
/*display: none;*/
}
.show{
z-index: 99;
background: #cc6600;
color: #fff;
}
.hide{
/*display: none;*/
z-index: 0;
}
</style>
</head>
<body>
<div class="nav">
<ul>
<li>导航一</li>
<li>导航二</li>
<li>导航三</li>
<li>导航四</li>
<li>导航五</li>
</ul>
</div>
<div class="container" id='container'>
<div class='show'>内容一</div>
<div>内容二</div>
<div>内容三</div>
<div>内容四</div>
<div>内容五</div>
</div>
</body>
<script>
window.onload=function(){
// 1、获取触发事件的元素
var oli = document.getElementsByTagName('li');
for (var i = 0; i < oli.length; i++) {
// 2、给触发元素添加触发事件
oli[i].onmouseover=function(){
// 3、调用函数,执行事件
change(this);
}
}
function change(obj){
var oli = document.getElementsByTagName('li');
var container = document.getElementById('container');
var oDiv = container.getElementsByTagName('div');
for (var i= 0; i< oli.length; i++) {
if(oli[i]==obj){
oDiv[i].className='show';
}else{
oDiv[i].className ='hide';
}
}
}
}
方案二、排他思想和对象思想
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.container{
width: 100%;
margin: 0 auto;
}
.body{
background: #cc6600;
width: 100%;
height: 500px;
}
.body div{
position: absolute;
background: #fff;
width: 50%;
height: 50%;
left: 25%;
top: 25%;
display: none;
}
.body div.show{
display: block;
}
</style>
</head>
<body>
<div class="container">
<button>导航一</button>
<button>导航二</button>
<button>导航三</button>
<button>导航四</button>
<button>导航五</button>
<button>导航六</button>
</div>
<div class="body" id='body'>
<div class='show'>内容一</div>
<div>内容二</div>
<div>内容三</div>
<div>内容四</div>
<div>内容五</div>
<div>内容六</div>
</div>
</body>
<script>
window.onload=function(){
// 1、获取元素
var btns = document.getElementsByTagName('button');
var body = document.getElementById('body');
var oDiv = body.getElementsByTagName('div');
// 2、遍历元素
for(var i = 0;i<btns.length;i++){
// 6、自定义属性,使触发元素与目标元素相关联
btns[i].index=i;
// 3、添加事件
btns[i].onclick=function(){
// 4、遍历清除样式
for (var i= 0; i< btns.length; i++) {
btns[i].className='';
oDiv[i].className='';
}
// 5、给目标元素设置样式
oDiv[this.index].className='show';
}
}
}
</script>
</html>

tab切换的两种方法的更多相关文章

  1. 原生JS—实现图片循环切换的两种方法

    今天我们主要讲讲如何使用原生JS实现图片的循环切换的方法.多余的话我们就不多说了,我们一个一个开始讲吧. 1  原生JS实现图片循环切换 -- 方法一 在上栗子之前我们先简单介绍一下所用的一些知识点. ...

  2. CSS实现导航条Tab切换的三种方法

    前面的话   导航条Tab在页面中非常常见,本文说详细介绍CSS实现导航条Tab的三种方法 布局   根据上图所示,先规定几个定义,上图的模块整体叫做导航,由导航标题和导航内容组成.要实现上图所示的布 ...

  3. 【原】react做tab切换的几种方式

    最近搞一个pc端的活动,搞了一个多月,甚烦,因为相比于pc端,更喜欢移动端多一点.因为移动端又能搞我的react了. 今天主要总结一下react当中tab切换的几种方式,因为tab切换基本上都会用到. ...

  4. Linux安装MySQL的两种方法

    转载:http://blog.csdn.net/superchanon/article/details/8546254/ 1.       运行平台:CentOS 6.3 x86_64,基本等同于RH ...

  5. hive权威安装出现的不解错误!(完美解决)两种方法都可以

    以下两种方法都可以,推荐用方法一! 方法一: 步骤一: yum -y install mysql-server 步骤二:service mysqld start 步骤三:mysql -u root - ...

  6. Response.Redirect 打开新窗体的两种方法

    普通情况下,Response.Redirect 方法是在server端进行转向,因此,除非使用 Response.Write("<script>window.location=' ...

  7. android fragment传递参数_fragment之间传值的两种方法

    在Activity中加载Fragment的时候.有时候要使用多个Fragment切换.并传值到另外一个Fragment.也就是说两个Fragment之间进行参数的传递.查了很多资料.找到两种方法.一种 ...

  8. Response.Redirect 打开这两种方法的一种新形式

    在一般情况下.Response.Redirect 该方法是在server年底转向,因此,除非 Response.Write("<script>window.location='h ...

  9. 两种方法上传本地文件到github

    https://www.jianshu.com/p/c70ca3a02087 自从使用github以来,一直都是在github网站在线上传文件到仓库中,但是有时因为网络或者电脑的原因上传失败.最重要的 ...

随机推荐

  1. Windows 下, SetTimer 定时器的研究.

    一直很困惑一个问题: 我设置了一个10秒的定时器,可是被调用的函数要花费30秒, 那待调用的函数第二次是什么时候调用的呢? 20秒, 40秒, 还是50秒呢.... 所以我进行了实验. 我写了一个类  ...

  2. 内存屏障(Memory barrier)-- 转发

    本文例子均在 Linux(g++)下验证通过,CPU 为 X86-64 处理器架构.所有罗列的 Linux 内核代码也均在(或只在)X86-64 下有效. 本文首先通过范例(以及内核代码)来解释 Me ...

  3. Add sharing to your app via UIActivityViewController

    http://www.codingexplorer.com/add-sharing-to-your-app-via-uiactivityviewcontroller/ April 4, 2014 Ev ...

  4. windows2003 iis+dedecms 4.0701版本,登录后台显示空白

    又可能是,i_user对 system32没有权限导致

  5. java常用类

    String 字符串类 System 可得到系统信息 Runtime类 StringBuilder(StringBuffer)类 Thread 线程类 Math 与数学有关的工具类 Date 日期类( ...

  6. 【Java学习笔记】<集合框架>TreeSet,Comparable,Comparator

    public class Person implements Comparable{ private String name; private int age; public Person(){ su ...

  7. Myeclipse加载maven

    1.自带,需要将之前下好的文本加入到myeclipse中即可.windows---->preference---->Myeclipse---->MavenMyeclipse 3.将本 ...

  8. processing学习笔记

    这是从http://funprogramming.org/视频学习过程中做的笔记,没法看视频的话,请FQ 点point(x,y); 线line(x,y,x2,y2); 背景background(x), ...

  9. SQL Server 利用锁提示优化Row_number()-程序员需知

    网站中一些老页面仍采用Row_number类似的开窗函数进行分页处理,此时如果遭遇挖坟帖的情形可能就需要漫长的等待且消耗巨大.这里给大家介绍根据Row_number()特性采用特定锁Hint提升查询速 ...

  10. 利用扩展事件(Xevents)捕捉高消耗查询

    生产环境中有时需要使用者抓取一些特定的语句分析,如超超长查询,或高IO查询等.一般来说大家对跟踪比较熟悉,主要因为其有完善的UI支持.由于扩展事件在sql2012才提供UI支持,所以虽然在08时就已经 ...