做个简单的tab切换效果,分别于jquery和js操作

(1)jQuery操作

先看下效果:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style type="text/css">
*{box-sizing:border-box;padding: 0;margin: 0}
ul{width: 100%;height: 50px;text-align: center;margin: 10px 0;}
ul li{list-style: none;height: 100%;width: 100px;
line-height: 50px;text-align: center;background-color: #9c8b8b;
margin: 0 6px;display: inline-block;cursor: pointer;}
section{width: 600px;height: 300px;border: 1px solid red;margin: 100px auto;}
#area{width: 400px;height: 200px;margin: 10px auto;border: 1px solid red;}
.child{width: 100%;height: 100%;float: left;display: none;text-align: center;line-height: 200px;font-size: 20px;color: black;}
.active{display: block}
#area .child:nth-child(1){
background-color: red;
}
#area .child:nth-child(2){
background-color: green;
}
#area .child:nth-child(3){
background-color: blue;
}
#area .child:nth-child(4){
background-color: yellow;
}
</style>
</head>
<body>
<section>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
<div id="area">
<div class="child active">
内容一
</div>
<div class="child">
内容二
</div>
<div class="child">
内容三
</div>
<div class="child">
内容四
</div>
</div>
</section>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
<script>
$(function(){
$("ul li").on('click',function(){
var index = $(this).index();
$(".child").each(function() {
if (index == $(this).index()) {
$(this).siblings().removeClass('active');
$(this).addClass('active');
}
})
})
})
</script>
</body>
</html>

(2)js操作

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
*{
padding: 0;
margin: 0;
}
li{
list-style: none;
float:left;
}
#tabCon {
clear: both;
}
#tabCon div {
display:none;
}
#tabCon div.fdiv {
display:block;
}
</style>
</head>
<body>
<div id="tanContainer">
<div id="tab">
<ul>
<li>标题一</li>
<li>标题二</li>
<li>标题三</li>
<li>标题四</li>
</ul>
</div>
<div id="tabCon">
<div class="fdiv">内容一</div>
<div>内容二</div>
<div>内容三</div>
<div>内容四</div>
</div>
</div>
</body>
<script>
var tabs=document.getElementById("tab").getElementsByTagName("li");
var divs=document.getElementById("tabCon").getElementsByTagName("div");
for(var i=0;i<tabs.length;i++){
tabs[i].onclick=function(){
change(this);
}
}
function change(obj){
for(var i=0;i<tabs.length;i++){
console.log(tabs[i])
if(tabs[i]==obj){
divs[i].className="fdiv";
}else{
divs[i].className="";
}
}
}
</script>
</html>

.

最新文章

  1. 用eclipse开发项目时遇到的常见错误整理,和配套解决方案(1)
  2. IOS的Crash情况在Crashlytics平台上统计解决方案的一点遗憾(截止到2015年6月14日)
  3. ImageUtil(验证码数据生成工具类)
  4. C#创建用户控件 - IPv4地址输入框
  5. android应用程序如何调用支付宝接口
  6. dispay属性的block,inline,inline-block
  7. [51单片机] TFT2.4彩屏2 [32*32文字显示]
  8. 微信公众平台开发之微信access_token如何有效长期保存
  9. iOS开发——app审核指导方针(官网)
  10. pyqt sender()学习
  11. 【百度地图API】你看过房产地图吗?你知道房产标注是如何建立的吗?
  12. wc的用法
  13. salesforce 零基础学习(六十六)VF页面应善于使用变量和函数(二)常用函数的使用
  14. php使用file_get_contents请求微信接口失败
  15. 1.10 tuple 元组
  16. 面向对象和面向过程,python中的类class,python中程序的入口——main方法,
  17. Android Gradle使用总结
  18. linux 服务器时间同步
  19. linux文件管理之proc文件系统
  20. this.class.getClassLoader().getResourceAsStream与this.class.getResourceAsStream 文件地址获取

热门文章

  1. linux中man手冊的高级使用方法
  2. Xcode9自带版本控制系统(Git)的使用
  3. 【转】Linux 查看CPU信息、机器型号等硬件信息
  4. Project Euler:Problem 42 Coded triangle numbers
  5. Android MaoZhuaWeiBo 好友动态信息列表数据抓取 -3
  6. BNU 13024 . Fi Binary Number 数位dp/fibonacci数列
  7. JAVA设计模式之:訪问者模式
  8. ubuntu安装phpstorm
  9. input如何去掉边框
  10. mybatis多个参数查询问题