用Div、CSS、JavaScript编写的Tab选项卡源代码,兼容所有浏览器

来源:互联网 发布:疯狂java培训怎么样 编辑:程序博客网 时间:2024/04/28 04:20

---------------------------------将以下代码复制,保存为css.css----------------------------------------

@charset "utf-8";
/* CSS Document */
#container
{
height:30px;
}
#tab1
{
float:left;
margin-left:100px;
width:100px;
height:30px;
border-left:1px #FF0000 solid;
border-right:1px #FF0000 solid;
border-top:1px #FF0000 solid;
background-color:#FF0000;
position:static;
}
#tab2
{
float:left;
margin-left:30px;
width:100px;
height:30px;
border-left:1px #FF0000 solid;
border-right:1px #FF0000 solid;
border-top:1px #FF0000 solid;
background-color:#FF0000;
position:relative;
}

#main
{
height:400px;
border:1px #FF0000 solid;
}

 

--------------------将以下代码复制,保存为a.html文件,与上面css文件放在同一文件夹----------------------

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link rel="stylesheet" type="text/css" href="css.css" />
<script language="javascript" type="text/javascript">
function change(tabId)
{
  for(var i=1;i<=2;i++)//将所有选项卡设问红色背景,有下边框
  {
   document.getElementById("tab"+i).style.position="static";
   document.getElementById("tab"+i).style.backgroundColor="#FF0000";   
  }
  //将选中的选项卡设为白色背景,无下边框
  document.getElementById("tab"+tabId).style.backgroundColor="#FFFFFF";
  document.getElementById("tab"+tabId).style.position="relative";
  document.getElementById("main").innerHTML="<br />你选择了第"+tabId+"个";
}

</script>
</head>

<body>
<div id="container">
 <div id="tab1" onclick="change(1)"></div>
    <div id="tab2" onclick="change(2)"></div>
   
</div>
<div id="main"><br />方角选项卡实例</div>

</body>
</html>
---------------------------------将以下代码复制,保存为css2.css----------------------------------------

@charset "utf-8";
/*选项卡容器*/
#container
{
height:30px;
}
/*选项卡1的容器*/
#tab1Container
{
height:30px;
width:100px;
float:left;
margin-left:30px;
background-color:#FFFFFF;
position:static;
}
/*选项卡1*/
#tab1
{
width:100px;
height:26px;
border-left:1px #FF0000 solid;
border-right:1px #FF0000 solid;
background-color:#ff0000;
}
/*用4个b叠加实现圆角*/
b
{
border-left:1px #FF0000 solid;
border-right:1px #FF0000 solid;
background-color:#ff0000;
}
.b1
{
display:block;
height:1px;
margin:0px 5px;
width:90px;
border-top:1px #FF0000 solid;
}
.b2
{
display:block;
height:1px;
margin:0px 3px;
width:94px;
}
.b3
{
display:block;
height:1px;
margin:0px 2px;
width:96px;
}
.b4
{
display:block;
height:1px;
margin:0px 1px;
width:98px;
}
/*选项卡2的容器*/
#tab2Container
{
height:30px;
width:100px;
float:left;
margin-left:30px;
background-color:#FFFFFF;
position:static;
}
/*选项卡2*/
#tab2
{
width:100px;
height:26px;
border-left:1px #FF0000 solid;
border-right:1px #FF0000 solid;
background-color:#FF0000;
}
/*内容div*/
#main
{
height:400px;
border:1px #FF0000 solid;
}

 

--------------------将以下代码复制,保存为b.html文件,与上面css文件放在同一文件夹----------------------

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link rel="stylesheet" type="text/css" href="css2.css" />
<script language="javascript" type="text/javascript">

function change(tabId)
{
  for(var i=1;i<=2;i++)
  {
   document.getElementById("tab"+i).style.position="static";//所有选项卡位置,底部不出现边框
   var div1=document.getElementById("tab"+i+"Container");
   var elem=div1.childNodes;
   for(var j=0;j<elem.length;j++)
   {
    elem[j].style.backgroundColor="#ff0000";//所有选项卡及圆角背景色
   }
  }
  var div1=document.getElementById("tab"+tabId+"Container");
  var elem=div1.childNodes;
  for(var i=0;i<elem.length;i++)
  {
  elem[i].style.backgroundColor="#ffffff";//选中的选项卡及圆角的背景色
  }
  document.getElementById("tab"+tabId+"Container").style.position="relative";//选中项的位置,底部出现边框
  document.getElementById("main").innerHTML="<br />你选择了第"+tabId+"个";
}
</script>
</head>

<body>
<div id="container">
    <div id="tab1Container">
        <b class="b1"></b>
        <b class="b2"></b>
        <b class="b3"></b>
        <b class="b4"></b>
        <div id="tab1" onclick="change(1)"></div>
  </div>
      <div id="tab2Container">
        <b class="b1"></b>
        <b class="b2"></b>
        <b class="b3"></b>
        <b class="b4"></b>
        <div id="tab2" onclick="change(2)"></div>
  </div>
</div>
<div id="main"><br />圆角选项卡实例</div>

</body>
</html>

 

---------------------------------将以下代码复制,保存为css3.css----------------------------------------

@charset "utf-8";
/*选项卡容器*/
#bodyDiv
{
width:100%;
height:400px;
}
#container
{
height:40px;
width:100px;
float:left;
}
/*选项卡1的容器*/
#tab1Container
{
margin-top:50px;
height:30px;
width:100px;
background-color:#FFFFFF;
position:static;
}
/*选项卡1*/
#tab1
{
width:100px;
height:26px;
border-left:1px #FF0000 solid;
background-color:#ff0000;
}
/*用4个b叠加实现圆角*/
b
{
border-left:1px #FF0000 solid;
background-color:#ff0000;
}
.b0
{
display:block;
height:1px;
margin-left:5px;
width:95px;
border-bottom:1px #FF0000 solid;
}
.b1
{
display:block;
height:1px;
margin-left:5px;
width:95px;
border-top:1px #FF0000 solid;
}
.b2
{
display:block;
height:1px;
margin-left:3px;
width:97px;
}
.b3
{
display:block;
height:1px;
margin-left:2px;
width:98px;
}
.b4
{
display:block;
height:1px;
margin-left:1px;
width:99px;
}
/*选项卡2的容器*/
#tab2Container
{
margin-top:30px;
height:30px;
width:100px;
background-color:#FFFFFF;
position:static;
}
/*选项卡2*/
#tab2
{
width:100px;
height:26px;
border-left:1px #FF0000 solid;
background-color:#FF0000;
}
/*内容div*/
#main
{
height:400px;
width:400px;
float:left;
border:1px #FF0000 solid;
}

 

 

 

--------------------将以下代码复制,保存为c.html文件,与上面css文件放在同一文件夹----------------------

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link rel="stylesheet" type="text/css" href="css3.css" />
<script language="javascript" type="text/javascript">

function change(tabId)
{
  for(var i=1;i<=2;i++)
  {
   document.getElementById("tab"+i).style.position="static";//所有选项卡位置,底部不出现边框
   var div1=document.getElementById("tab"+i+"Container");
   var elem=div1.childNodes;
   for(var j=0;j<elem.length;j++)
   {
    elem[j].style.backgroundColor="#ff0000";//所有选项卡及圆角背景色
   }
  }
  var div1=document.getElementById("tab"+tabId+"Container");
  var elem=div1.childNodes;
  for(var i=0;i<elem.length;i++)
  {
  elem[i].style.backgroundColor="#ffffff";//选中的选项卡及圆角的背景色
  }
  document.getElementById("tab"+tabId+"Container").style.position="relative";//选中项的位置,底部出现边框
  document.getElementById("main").innerHTML="<br />你选择了第"+tabId+"个";
}
</script>
</head>

<body>
<div id="bodyDiv">
 <div id="container">
  <div id="tab1Container">
   <b class="b1"></b>
   <b class="b2"></b>
   <b class="b3"></b>
   <b class="b4"></b>
   <div id="tab1" onclick="change(1)"></div>
   <b class="b4"></b>
   <b class="b3"></b>
   <b class="b2"></b>
   <b class="b0"></b>
   </div>
    <div id="tab2Container">
   <b class="b1"></b>
   <b class="b2"></b>
   <b class="b3"></b>
   <b class="b4"></b>
   <div id="tab2" onclick="change(2)"></div>
   <b class="b4"></b>
   <b class="b3"></b>
   <b class="b2"></b>
   <b class="b0"></b>
   </div>
 </div>
 <div id="main"><br />圆角选项卡实例</div>
</div>
</body>
</html>

---------------------------------完----------------------------------

引用请注明出处

 

 

 

原创粉丝点击