【发布时间】:2018-07-18 06:16:15
【问题描述】:
我正在尝试使用 html css java script 创建选项卡。单击选项卡时,它会转到相应的选项卡内容。在此代码中 点击时 GO TO TAB2 转到选项卡 2。 GO TO TAB 转到上一个选项卡。
如何做到这一点?
window.onload=function() {
// get tab container
var container = document.getElementById("tabContainer");
var tabcon = document.getElementById("tabscontent");
//alert(tabcon.childNodes.item(1));
// set current tab
var navitem = document.getElementById("tabHeader_1");
//store which tab we are on
var ident = navitem.id.split("_")[1];
//alert(ident);
navitem.parentNode.setAttribute("data-current",ident);
//set current tab with class of activetabheader
navitem.setAttribute("class","tabActiveHeader");
//hide two tab contents we don't need
var pages = tabcon.getElementsByTagName("div.tabpage");
for (var i = 1; i < pages.length; i++) {
pages.item(i).style.display="none";
}
//this adds click event to tabs
var tabs = container.getElementsByTagName("li");
for (var i = 0; i < tabs.length; i++) {
tabs[i].onclick=displayPage;
}
// get tab container 2
var container2 = document.getElementById("tabContainer2");
var tabcon2 = document.getElementById("tabscontent2");
var navitem2 = document.getElementById("tabHeader2_1");
//store which tab we are on
var ident2 = navitem2.id.split("_")[1];
//alert(ident2);
navitem2.parentNode.setAttribute("data-current",ident2);
//set current tab with class of activetabheader
navitem2.setAttribute("class","tabActiveHeader");
//hide two tab contents we don't need
var pages2 = tabcon2.getElementsByTagName("div.tabpage2");
for (var i = 1; i < pages2.length; i++) {
pages2.item(i).style.display="none";
}
//this adds click event to tabs
var tabs2 = container2.getElementsByTagName("li");
for (var i = 0; i < tabs2.length; i++) {
tabs2[i].onclick=displayPage2;
}
// get tab container 3
var container3 = document.getElementById("tabContainer3");
var tabcon3 = document.getElementById("tabscontent3");
//alert("TAB3: "+tabcon3.childNodes.item(1));
// set current tab
var navitem3 = document.getElementById("tabHeader3_1");
//store which tab we are on
var ident3 = navitem3.id.split("_")[1];
//alert(ident);
navitem3.parentNode.setAttribute("data-current",ident3);
//set current tab with class of activetabheader
navitem3.setAttribute("class","tabActiveHeader");
//hide two tab contents we don't need
var pages3 = tabcon3.getElementsByTagName("div.tabpage3");
for (var i = 1; i < pages3.length; i++) {
pages3.item(i).style.display="none";
}
//this adds click event to tabs
var tabs3 = container3.getElementsByTagName("li");
for (var i = 0; i < tabs3.length; i++) {
tabs3[i].onclick=displayPage3;
}
};
// on click of first tabs
function displayPage(event) {
for (var i = 0; i < event.target.parentElement.childElementCount; i++) {
if(event.target.parentElement.children[i].id == event.target.id)
{
document.getElementById(event.target.id).setAttribute("class","tabActiveHeader");
document.getElementById(document.getElementsByClassName('tabpage')[i].id).style.display="block";
}
else
{
document.getElementById(event.target.parentElement.children[i].id).removeAttribute("class");
document.getElementById(document.getElementsByClassName('tabpage')[i].id).style.display="none";
}
}
}
//Tab 2
function displayPage2(event) {
for (var i = 0; i < event.target.parentElement.childElementCount; i++) {
if(event.target.parentElement.children[i].id == event.target.id)
{
document.getElementById(event.target.id).setAttribute("class","tabActiveHeader");
document.getElementById(document.getElementsByClassName('tabpage2')[i].id).style.display="block";
}
else
{
document.getElementById(event.target.parentElement.children[i].id).removeAttribute("class");
document.getElementById(document.getElementsByClassName('tabpage2')[i].id).style.display="none";
}
}
}
//Tab 3
function displayPage3(event) {
for (var i = 0; i < event.target.parentElement.childElementCount; i++) {
if(event.target.parentElement.children[i].id == event.target.id)
{
document.getElementById(event.target.id).setAttribute("class","tabActiveHeader");
document.getElementById(document.getElementsByClassName('tabpage3')[i].id).style.display="block";
}
else
{
document.getElementById(event.target.parentElement.children[i].id).removeAttribute("class");
document.getElementById(document.getElementsByClassName('tabpage3')[i].id).style.display="none";
}
}
}
function goToTab2() {
document.getElementById("tabHeader_1").removeAttribute("class","tabActiveHeader");
document.getElementById("tabHeader_3").removeAttribute("class","tabActiveHeader");
document.getElementById("tabHeader_2").setAttribute("class","tabActiveHeader");
document.getElementById("tabpage_1").setAttribute("style", "display: none");
document.getElementById("tabpage_3").setAttribute("style", "display: none");
document.getElementById("tabpage_2").setAttribute("style", "display: block");
}
function goToTab1() {
document.getElementById("tabHeader_2").removeAttribute("class","tabActiveHeader");
document.getElementById("tabHeader_3").removeAttribute("class","tabActiveHeader");
document.getElementById("tabHeader_1").setAttribute("class","tabActiveHeader");
document.getElementById("tabpage_2").setAttribute("style", "display: none");
document.getElementById("tabpage_3").setAttribute("style", "display: none");
document.getElementById("tabpage_1").setAttribute("style", "display: block");
}
body {
font-family: arial;
}
.tabContainer {
padding:15px;
-moz-border-radius: 4px;
border-radius: 4px;
}
.tabs{
overflow:hidden;
}
.tabs > ul{
font: 1em;
list-style:none;
}
.tabs > ul > li {
margin:0 2px 0 0;
padding:7px 10px;
display:block;
float:left;
color:#333;
-webkit-user-select: none;
-moz-user-select: none;
user-select: none;
-moz-border-radius-topleft: 4px;
-moz-border-radius-topright: 4px;
-moz-border-radius-bottomright: 0px;
-moz-border-radius-bottomleft: 0px;
border-top-left-radius:4px;
border-top-right-radius: 4px;
border-bottom-right-radius: 0px;
border-bottom-left-radius: 0px;
background: #FFFFFF; /* old browsers */
background: -moz-linear-gradient(top, #FFFFFF 0%, #F3F3F3 10%, #F3F3F3 50%, #FFFFFF 100%); /* firefox */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#FFFFFF), color-stop(10%,#F3F3F3), color-stop(50%,#F3F3F3), color-stop(100%,#FFFFFF)); /* webkit */
cursor:pointer;
border: 1px #ccc solid;
}
.tabs > ul > li:hover {
color:#ccc;
background: #C9C9C9; /* old browsers */
background: -moz-linear-gradient(top, #0C91EC 0%, #257AB6 100%); /* firefox */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#0C91EC), color-stop(100%,#257AB6)); /* webkit */
}
.tabs > ul > li.tabActiveHeader {
color:#FFF;
color: #333background: #C9C9C9; /* old browsers */
background: -moz-linear-gradient(top, #0C91EC 0%, #257AB6 100%); /* firefox */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#0C91EC), color-stop(100%,#257AB6)); /* webkit */
}
.tabscontent {
-moz-border-radius-topleft: 0px;
-moz-border-radius-topright: 4px;
-moz-border-radius-bottomright: 4px;
-moz-border-radius-bottomleft: 4px;
border-top-left-radius: 0px;
border-top-right-radius: 4px;
border-bottom-right-radius: 4px;
border-bottom-left-radius: 4px;
padding:10px 10px 25px;
background: #FFFFFF; /* old browsers */
background: -moz-linear-gradient(top, #FFFFFF 0%, #FFFFFF 90%, #e4e9ed 100%); /* firefox */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#FFFFFF), color-stop(90%,#FFFFFF), color-stop(100%,#e4e9ed)); /* webkit */
margin:0;
color:#333;
border: 1px #ccc solid;
}
.gototab {
cursor: pointer;
background: green;
color: #fff;
padding: 10px;
height: 30px;
line-height: 30px;
width: 100px;
text-align: center;
float: left;
margin: 10px;
}
<div id="tabContainer" class="tabContainer">
<div class="tabs" id="tabs">
<ul>
<li id="tabHeader_1">Page 1</li>
<li id="tabHeader_2">Page 2</li>
<li id="tabHeader_3">Page 3</li>
</ul>
</div>
<div id="tabscontent" class="tabscontent">
<div class="tabpage" id="tabpage_1">
<h2>Page 1</h2>
<p>Lorem Ipsum is simply dummy text </p>
</div>
<div class="tabpage" id="tabpage_2" style="display:none;">
<h2>Page 2</h2>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
</p>
</div>
<div class="tabpage" id="tabpage_3" style="display:none;">
<h2>Page 3</h2>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
</p>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
</p>
</div>
</div>
</div>
<div class="gototab" onclick="goToTab2(this)">Go to Tab 2 >> </div>
<div class="gototab" onclick="goToTab1(this)"> << Go to Tab 1</div>
<div style="clear:both;"> <div>
<p style="margin-top: 150px;"></p>
我想使用 java 脚本添加下一个上一个按钮以在选项卡之间切换。
【问题讨论】:
-
您可以为所有选项卡赋予
tabIndex之类的属性并创建通用选项卡选择器,而不是选择固定的ids。这样你就可以为下一个/上一个添加或减去一个。
标签: javascript html css