美文网首页
点击切换标签页的两种方式

点击切换标签页的两种方式

作者: 积_渐 | 来源:发表于2018-09-04 09:40 被阅读3次
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<style>
*{
margin:0;
padding: 0;
}
#tab li{
float: left;
list-style: none;
width: 80px;
height: 40px;
line-height: 40px;
cursor: pointer;
text-align: center;
}
#container{
position: relative;
}
#content1,#content2,#content3{
width: 300px;
height: 100px;
padding:30px;
position: absolute;
top: 40px;
left: 0;
}
#tab1,#content1{
background-color: #ffcc00;
}
#tab2,#content2{
background-color: #ff00cc;
}
#tab3,#content3{
background-color: #00ccff;
}
</style>
<body>
    <ul id="tab">
        <li id="tab1" onclick="show(1)">1111</li>
        <li id="tab2" onclick="show(2)">2222</li>
        <li id="tab3" onclick="show(3)">3333</li>
    </ul>
    <div id="container">
    <div id="content1" style="z-index: 1;">
    11111
    </div>
    <div id="content2">
    2222
    </div>
    <div id="content3">
    3333
    </div>
    </div>
</body>
<script>
function show(n){
var curr=document.querySelector("div[style]");
curr.removeAttribute("style");
document.getElementById("content"+n)
.style.zIndex="1";
}
</script>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="http://www.topworld.cn/dist/new/js/jquery-1.11.0.min.js"></script>

</head>
<style>
*{
margin:0;
padding: 0;
}
#tab li{
float: left;
list-style: none;
width: 80px;
height: 40px;
line-height: 40px;
cursor: pointer;
text-align: center;
}
#container{
position: relative;
}
#content1,#content2,#content3{
width: 300px;
height: 100px;
padding:30px;
position: absolute;
top: 40px;
left: 0;
}
#tab1,#content1{
background-color: #ffcc00;
}
#tab2,#content2{
background-color: #ff00cc;
}
#tab3,#content3{
background-color: #00ccff;
}
</style>
<body>
    <ul id="tab">
        <li id="tab1" value="1">1111</li>
        <li id="tab2" value="2">2222</li>
        <li id="tab3" value="3">3333</li>
    </ul>
    <div id="container">
    <div id="content1" style="z-index: 1;">
    1111
    </div>
    <div id="content2">
    2222
    </div>
    <div id="content3">
    3333
    </div>
    </div>
</body>
<script>
$("#tab>li").click(function(){
console.log(this);
$("#content"+$(this).val()).attr('style','z-index:1').siblings("div").removeAttr("style");
});
</script>
</html>

相关文章

网友评论

      本文标题:点击切换标签页的两种方式

      本文链接:https://www.haomeiwen.com/subject/zyawwftx.html