qq_面朝大海_achNT1
2018-10-10 21:09
<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>房产</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>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>实践题 - 选项卡</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实现选项卡切换
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">房产</button>
<button type="button" name="btn-li">家居</button>
<button type="button" name="btn-li">二手房</button>
<ul id="text">
<li class="active">
275万购昌平邻铁三居总价20万买一居<br/>
200万内购五环三居140万安家东三环<br/>
北京首现零首付楼盘53万购东5环50平<br/>
京楼盘直降5000中信府公园楼王现房<br/>
</li>
<li>
40平出租屋大改造 美少女的混搭小窝<br/>
经典清新简欧爱家 90平老房焕发新生<br/>
新中式的酷色温情 66平撞色活泼家居<br/>
瓷砖就像选好老婆 卫生间烟道的设计<br/>
</li>
<li>
通州豪华3居260万 二环稀缺2居250w甩<br/>
西3环通透2居290万 130万2居限量抢购<br/>
黄城根小学学区仅260万 121平70万抛!<br/>
独家别墅280万 苏州桥2居优惠价248万<br/>
</li>
</ul>
</section>
</body>
</html>
JavaScript进阶篇
468060 学习 · 21891 问题
相似问题