TAB的JS切换效果怎么做

JavaScript015

TAB的JS切换效果怎么做,第1张

楼主 你的代码不全 我给你重新写一个吧

<!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>

    <title>tab页切换</title>

    <meta http-equiv="Content-Type" content="text/html charset=utf-8" />

    <script type="text/javascript">

        function switchTab(ProTag, ProBox) {

            for (i = 1 i < 5 i++) {

                if ("tab" + i == ProTag) {

                    document.getElementById(ProTag).getElementsByTagName("a")[0].className = "on"

                } else {

                    document.getElementById("tab" + i).getElementsByTagName("a")[0].className = ""

                }

                if ("con" + i == ProBox) {

                    document.getElementById(ProBox).style.display = ""

                } else {

                    document.getElementById("con" + i).style.display = "none"

                }

            }

        }

    </script>

    <style type="text/css">

        *

        {

            padding: 0

            margin: 0

            line-height: 25px

            font-size: 12px

            list-style:none

        }

        #tabContainer

        {

            margin: 30px

        }

        #tabContainer li

        {

            float: left

            width: 80px

            margin: 0 3px

            background: #efefef

            text-align: center

        }

        #tabContainer a

        {

            display: block

        }

        #tabContainer a.on

        {

            background: pink

        }

    </style>

</head>

<body>

    <div id="tabContainer">

        <ul>

            <li id="tab1"><a href="#" class="on" onclick="switchTab('tab1','con1')this.blur()return false">

                命运</a></li>

            <li id="tab2"><a href="#" onclick="switchTab('tab2','con2')this.blur()return false">

                运势</a></li>

            <li id="tab3"><a href="#" onclick="switchTab('tab3','con3')this.blur()return false">

                缘分</a></li>

            <li id="tab4"><a href="#" onclick="switchTab('tab4','con4')this.blur()return false">

                人生</a></li>

        </ul>

        <div style="clear: both">

        </div>

        <div id="con1">

            命运是个神马玩意儿...

        </div>

        <div id="con2" style="display: none">

            霉运也是一种运气...

        </div>

        <div id="con3" style="display: none">

            缘份纯粹是蒙人的东西...

        </div>

        <div id="con4" style="display: none">

            人生就是人"生"出来之后的过程...

        </div>

    </div>

</body>

</html>

代码如下:

<html> 

<head> 

<style type="text/css"> 

padding-bottom: 0px 

margin: 0px 

padding-left: 0px 

padding-right: 0px 

font-size: 12px 

padding-top: 0px 

BODY 

padding-left: 20px 

padding-top: 20px 

.wid240 

width: 242px 

margin-bottom: 20px 

.wid180 

width: 182px 

.tab 

border-bottom: #000 1px solid 

border-left: #000 1px solid 

border-top: #000 1px solid 

border-right: #000 1px solid 

.tab UL 

zoom: 1 

clear: both 

.tab UL:after 

display: block 

height: 0px 

visibility: hidden 

clear: both 

content: "" 

.tab UL LI 

text-align: center 

line-height: 26px 

width: 60px 

display: inline 

background: #000 

float: left 

height: 26px 

color: #fff 

.tab UL LI.on 

background: #fff 

color: #000 

.tabList 

border-bottom: #000 1px solid 

border-left: #000 1px solid 

height: 150px 

border-top: #000 1px 

border-right: #000 1px solid 

.tabList .one 

padding-bottom: 10px 

padding-left: 10px 

padding-right: 10px 

display: none 

color: #ff0000 

padding-top: 10px 

.tabList .block 

display: block 

</style> 

<script type="text/javascript"> 

function setTab(name,m,n){ 

for( var i=1i<=ni++){ 

var menu = document.getElementById(name+i) 

var showDiv = document.getElementById("cont_"+name+"_"+i) 

menu.className = i==m ?"on":"" 

showDiv.style.display = i==m?"block":"none" 

</script> 

<meta name="GENERATOR" content="MSHTML 8.00.7600.16535"> 

</head> 

<body> 

<div class="wid240"> 

<div class="tab"> 

<ul> 

<li id="one1" class="on" onmouseover='setTab("one",1,4)'>one1 </li> 

<li id="one2" onmouseover='setTab("one",2,4)'>one2 </li> 

<li id="one3" onmouseover='setTab("one",3,4)'>one3 </li> 

<li id="one4" onmouseover='setTab("one",4,4)'>one4 </li> 

</ul> 

</div> 

<div class="tabList"> 

<div id="cont_one_1" class="one block"> 

cont_one_1</div> 

<div id="cont_one_2" class="one"> 

cont_one_2</div> 

<div id="cont_one_3" class="one"> 

cont_one_3</div> 

<div id="cont_one_4" class="one"> 

cont_one_4</div> 

</div> 

</div> 

<div class="wid180"> 

<div class="tab"> 

<ul> 

<li id="tow1" class="on" onclick='setTab("tow",1,3)'>tow1 </li> 

<li id="tow2" onclick='setTab("tow",2,3)'>tow2 </li> 

<li id="tow3" onclick='setTab("tow",3,3)'>tow3 </li> 

</ul> 

</div> 

<div class="tabList"> 

<div id="cont_tow_1" class="one block"> 

cont_tow_1</div> 

<div id="cont_tow_2" class="one"> 

cont_tow_2</div> 

<div id="cont_tow_3" class="one"> 

cont_tow_3</div> 

</div> 

</div> 

</body> 

</html>

直接复制到本地html页面运行就可以,望采纳,谢谢!

这里分享下js判断当前选中哪个是tab页的方法。

设备:联想电脑

系统:win7

软件:Visual Studio 2017

1、首先来看一下要实现的效果,如下图所示,顶上是tab页卡,下面是内容。

2、然后准备页面的内容,如下图所示,tab运用ul布局,内容运用div布局。

3、接下来就是去除页面中的一些默认间距以及ul的list-style样式,如下图所示。

4、然后定义tab页卡以及内容的相关样式,如下图所示。

5、接着导入所需要的jquery的压缩库文件,如下图所示,版本大家自己选择。

6、最后就是实现选择一个页卡,然后展现页卡内内容的方法,JS实现tab页卡主要是运用了mouseenter的事件方法进行控制显示。