js+css实现tab菜单切换效果的方法
作者:bea
本文实例讲述了js+css实现tab菜单切换效果的方法。分享给大家供大家参考。具体实现方法如下: index.css如下: 代码如下: * { margin: 0px; padding: 0px; } body { width: 600px; margin: 0 auto; background-color: silver; } #contanier { background-color: yellow; w
本文实例讲述了js+css实现tab菜单切换效果的方法。分享给大家供大家参考。具体实现方法如下:
index.css如下:
代码如下:
* {
margin: 0px;
padding: 0px;
}
body {
width: 600px;
margin: 0 auto;
background-color: silver;
}
#contanier {
background-color: yellow;
width: 600px;height: 400px;
}
#tabNavi {
width: 600px;height: 30px;
background-color: #00bfff;
text-decoration: none;
list-style-type: none;
}
#tabNavi li {
float: left;
margin-right: 7px;
background-color: #008b8b;
color: white;
cursor: pointer;
width: 60px;
height: 28px;
line-height: 30px;
text-align: center;
}
#content {
width: 600px;height: 370px;
background-color: white;
}
index.html如下:
代码如下:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>js实现tab菜单动态切换效果</title>
<link href="css/index.css" rel="stylesheet" />
<script type="text/javascript">
function gel(id) {
return document.getElementById(id);
}
var arr = [
{ "title": "新闻", "content": "这是新闻频道" },
{ "title": "财经", "content": "这是财经频道" },
{ "title": "娱乐", "content": "这是娱乐频道" },
{ "title": "体育", "content": "这是体育频道" },
{ "title": "汽车", "content": "这是汽车频道" },
{ "title": "视频", "content": "这是视频频道" },
{ "title": "生活", "content": "这是生活频道" }
];
window.onload = function() {
for (var i = 0; i < arr.length; i++) {
var liNew = document.createElement("li");
liNew.innerHTML = arr[i].title;
gel("tabNavi").appendChild(liNew);
//在这些li上面都绑定点击事件,就需要给他们每一个赋一个属性(最好是id)
liNew.setAttribute("id", i);
liNew.onclick = function () {
var navs = gel("tabNavi").childNodes;
//清除所有颜色
for (var j = 0; j < navs.length; j++) {
if (navs[j].nodeType == 1) {
navs[j].style.backgroundColor ="#008b8b";
}
}
this.style.backgroundColor = "red";
gel("content").innerHTML = arr[this.id].content;
};
}
};
</script>
</head>
<body>
<div id="contanier">
<ul id="tabNavi"></ul>
<div id="content" class="content"></div>
</div>
</body>
</html>
希望本文所述对大家的javascript程序设计有所帮助。
有用 | 无用
index.css如下:
代码如下:
* {
margin: 0px;
padding: 0px;
}
body {
width: 600px;
margin: 0 auto;
background-color: silver;
}
#contanier {
background-color: yellow;
width: 600px;height: 400px;
}
#tabNavi {
width: 600px;height: 30px;
background-color: #00bfff;
text-decoration: none;
list-style-type: none;
}
#tabNavi li {
float: left;
margin-right: 7px;
background-color: #008b8b;
color: white;
cursor: pointer;
width: 60px;
height: 28px;
line-height: 30px;
text-align: center;
}
#content {
width: 600px;height: 370px;
background-color: white;
}
index.html如下:
代码如下:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>js实现tab菜单动态切换效果</title>
<link href="css/index.css" rel="stylesheet" />
<script type="text/javascript">
function gel(id) {
return document.getElementById(id);
}
var arr = [
{ "title": "新闻", "content": "这是新闻频道" },
{ "title": "财经", "content": "这是财经频道" },
{ "title": "娱乐", "content": "这是娱乐频道" },
{ "title": "体育", "content": "这是体育频道" },
{ "title": "汽车", "content": "这是汽车频道" },
{ "title": "视频", "content": "这是视频频道" },
{ "title": "生活", "content": "这是生活频道" }
];
window.onload = function() {
for (var i = 0; i < arr.length; i++) {
var liNew = document.createElement("li");
liNew.innerHTML = arr[i].title;
gel("tabNavi").appendChild(liNew);
//在这些li上面都绑定点击事件,就需要给他们每一个赋一个属性(最好是id)
liNew.setAttribute("id", i);
liNew.onclick = function () {
var navs = gel("tabNavi").childNodes;
//清除所有颜色
for (var j = 0; j < navs.length; j++) {
if (navs[j].nodeType == 1) {
navs[j].style.backgroundColor ="#008b8b";
}
}
this.style.backgroundColor = "red";
gel("content").innerHTML = arr[this.id].content;
};
}
};
</script>
</head>
<body>
<div id="contanier">
<ul id="tabNavi"></ul>
<div id="content" class="content"></div>
</div>
</body>
</html>
希望本文所述对大家的javascript程序设计有所帮助。
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- js实现class样式的修改、添加及删除的方法
- DOM基础教程之事件类型
- js使用DOM设置单选按钮、复选框及下拉菜单的方法
- DOM基础教程之事件对象
- js判断浏览器版本以及浏览器内核的方法
- js实现的捐赠管理完整实例
- JavaScript可视化图表库D3.js API中文参考
- JS自定义对象实现Java中Map对象功能的方法
- JavaScript数组Array对象增加和删除元素方法总结
- DOM基础教程之使用DOM + Css
- jquery+ajax实现跨域请求的方法
- 嵌入式iframe子页面与父页面js通信的方法
- javascript浏览器窗口之间传递数据的方法
- 用队列模拟jquery的动画算法实例
- jQuery 处理页面的事件详解
- DOM 事件流详解
- jQuery链使用指南
- 使用jQuery管理选择结果
- javascript动态修改Li节点值的方法