琢磨出的一個(gè)寫法
<style type="text/css">
? ? ? * {
? ? ? ? padding: 0;
? ? ? ? margin: 0;
? ? }
? ? .box>div {
? ? ? ? width: 300px;
? ? ? ? height: 300px;
? ? ? ? border: 1px solid black;
? ? ? ? display: none;
? ? }
? ? .box {
? ? ? ? position: relative;
? ? }
? ? .box div:nth-child(2) {
? ? ? ? position: absolute;
? ? ? ? top: 0;
? ? ? ? background: green;
? ? }
? ? .box div:nth-child(3) {
? ? ? ? position: absolute;
? ? ? ? top: 0;
? ? ? ? background: salmon;
? ? }
? ? .show {
? ? ? ? display: block !important;
? ? ? ? background: tomato;
? ? }
? ? #list {
? ? ? ? width: 300px;
? ? ? ? height: 50px;
? ? ? ? border: 1px solid black;
? ? ? ? /* margin: 0 auto; */
? ? ? ? text-align: center;
? ? }
? ? #list ul {
? ? ? ? list-style-type: none;
? ? }
? ? #list ul li {
? ? ? ? float: left;
? ? ? ? margin: 0px 16px;
? ? ? ? cursor: pointer;
? ? ? ? height: 50px;
? ? ? ? text-align: center;
? ? ? ? border-right: 1px solid black;
? ? }
? ? .active {
? ? ? ? background: #ccc;
? ? }
? ? ? ?
? ? ? ?
? ? </style>
? ?
?
</head>
<body>
?<div id="list">
? ? ? ? <ul>
? ? ? ? ? ? <li class=a ctive>房產(chǎn)</li>
? ? ? ? ? ? <li>家具</li>
? ? ? ? ? ? <li>二手房</li>
? ? ? ? </ul>
? ? </div>
? ? <div class="box">
? ? ? ? <div class="show">1111</div>
? ? ? ? <div>22</div>
? ? ? ? <div>333</div>
? ? </div>
?
</body>
<script>
? ? function Tabs() {
? ? this. tli = document.querySelectorAll('#list>ul>li')
? ? this. tdiv = document.querySelectorAll('.box>div')
}
Tabs.prototype.fn = function () {
? ? var that = this
? ? for (var i = 0; i < this.tli.length; i++) {
? ? ? ? this.tli[i].index = i;
? ? ? ? this .tli[i].onclick = function () {
? ? ? ? ? ? // console.log(this)
? ? ? ? ? ? that.myclick(this);//
? ? ? ? ? ??
? ? ? ? }
? ? }
}
Tabs.prototype.myclick = function (that) {
? ? for (var j = 0; j < this.tdiv.length; j++) {
? ? ? ? this.tdiv[j].setAttribute('class', '')
? ? ? ? this.tli[j].setAttribute('class', '')
? ? }
? ? that.setAttribute('class', 'active')
? ? this.tdiv[that.index].setAttribute('class', 'show')
}
var tabs = new Tabs()
tabs.fn()
</script>
2018-10-11
<!DOCTYPE html>
<html>
<head>
? ? <meta charset="UTF-8">
? ? <title>實(shí)踐題 - 選項(xiàng)卡</title>
? ? <style type="text/css">
? ? ?/* CSS樣式制作 */??
? ? ?*{
margin:0;
padding:0;
}
body{
background-color:#DCDCDC;
}
#box{
margin:20px;
width:340px;
height:220px;
background-color:#fafafa;
padding:10px 20px;
box-sizing:border-box;
}
button{
width:60px;
height:30px;
background-color:#fafafa;
outline:none;
border:1px solid #DCDCDC;
cursor:pointer;
position:relative;
top:3px;
border-bottom:none;
left:5px;
box-sizing:border-box;
}
#text{
list-style-type:none;
width:260px;
height:160px;
border:1px solid lightblue;
border-top:2px solid #8B4513;
box-sizing:border-box;
position:relative;
z-index:0;
}
li{
position:absolute;
left:5px;
top:10px;
line-height:26px;
font-size:12px;
display:none;
}
.active{
display:block;
}
.zIdx{
z-index:1;
border-top:2px solid #8B4513;
}? ?
? ? ? ?
? ? </style>
? ? <script type="text/javascript">
? ? ? ? ?
? ? // JS實(shí)現(xiàn)選項(xiàng)卡切換
? ? window.onload=function(){
? ? ?var text = document.getElementById('text');
var lis = document.getElementsByTagName('li');
var btns = document.getElementsByTagName('button');
var box = document.getElementById('box');
// 遍歷
function each(array,fn){
for(var i=0;i<array.length;i++){
fn(i,array[i]);
}
}
each(btns,function(index,ele){
ele.onclick = function(){
classMethod.addClass(index,ele)
}
})
var classMethod = {
clearClass:function (className,parentNode){
var pattern = new RegExp('(^|\\s+)' + className + '($|\\s+)');
var allNodes = parentNode.getElementsByTagName('*');
parentNode = parentNode || document;
for(var i = 0;i<allNodes.length;i++){
if(pattern.test(allNodes[i].className)){
allNodes[i].className ="";
}
}
},
addClass:function (index,ele){
if(lis[index].className){
return;
}else if(!lis[index].className){
classMethod.clearClass("active",text);
classMethod.clearClass('zIdx',box);
lis[index].className = "active";
ele.className ="zIdx";
}
}
}? ??
? ? }
? ??
? ? </script>
?
</head>
<body>
<!-- HTML頁面布局 -->
<section id="box">
<button type="button" name="btn-li" class="zIdx">房產(chǎn)</button>
<button type="button" name="btn-li">家居</button>
<button type="button" name="btn-li">二手房</button>
<ul id="text">
<li class="active">
275萬購昌平鄰鐵三居總價(jià)20萬買一居<br/>
200萬內(nèi)購五環(huán)三居140萬安家東三環(huán)<br/>
北京首現(xiàn)零首付樓盤53萬購東5環(huán)50平<br/>
? ? ?京樓盤直降5000中信府公園樓王現(xiàn)房<br/>
</li>
<li>
40平出租屋大改造 美少女的混搭小窩<br/>
? ? ? 經(jīng)典清新簡(jiǎn)歐愛家 90平老房煥發(fā)新生<br/>
? ? ? 新中式的酷色溫情 66平撞色活潑家居<br/>
? ? ? 瓷磚就像選好老婆 衛(wèi)生間煙道的設(shè)計(jì)<br/>
? ? ? </li>
<li>
通州豪華3居260萬 二環(huán)稀缺2居250w甩<br/>
? ? ? 西3環(huán)通透2居290萬 130萬2居限量搶購<br/>
? ? ? 黃城根小學(xué)學(xué)區(qū)僅260萬 121平70萬拋!<br/>
? ? ? 獨(dú)家別墅280萬 蘇州橋2居優(yōu)惠價(jià)248萬<br/>
? ? ? </li>
</ul>
</section>
?
</body>
</html>