<js>选项卡

OOA:点击按钮显示对应内容 (通过索引一一对应)

 1. 布局

 2.获取元素 设置默认索引

 3.点击事件

 4.获取上一个索引

 5.隐藏上一个内容

6.获取下一个索引

7.显示下一个内容

(1)HTML部分

 <div class="box">
        <ul>
            <li class="active">热门推荐</li>
            <li>限时抢购</li>
            <li>积分兑换</li>
            <li>满减返现</li>
        </ul>
        <div class="cont">
            <div class="cont1">
                一堆各种复杂的页面结构1
            </div>
            <div class="cont2">
                一堆各种复杂的页面结构2
            </div>
            <div class="cont3">
                一堆各种复杂的页面结构3
            </div>
            <div class="cont4">
                一堆各种复杂的页面结构4
            </div>
        </div>
    </div>

(2)CSS部分

.box {
            width: 400px;
            height: 300px;
            border: solid 1px black;
            margin: 0 auto;
        }
        .box ul {
            margin: 0;
            padding: 0;
            list-style: none;
            display: flex;
            height: 40px;
            line-height: 40px;
            text-align: center;
            border-bottom: solid 1px black
        }     
        .box li {
            flex: 1;
            border-left: solid 1px black;
            border-right: solid 1px black;
            cursor: pointer;
        }
        /* 点击时的样式 */      
        .box li:active {
            background: #99f
        }
        /* class的样式 */       
        .box li.active {
            background: red
        }   
        .cont div {
            height: 259px;
            display: none;
        }  
        .cont .cont1 {
            background: #66f;
            display: block
        } 
        .cont .cont2 {
            background: #6f6;
        }
        .cont .cont3 {
            background: #f66;
        } 
        .cont .cont4 {
            background: #ff6;
        }

(3)JS部分 

 function Tab() {
        this.li = document.querySelectorAll(".box li");
        this.div = document.querySelectorAll(".cont>div");
        // 默认值 是li的索引,默认第0个选中
        this.Index = 0;
        this.addEvent();
    }
    Tab.prototype.addEvent = function() {
        // 获取正确的this给点击事件
        const that = this;
        // 遍历 给每个li绑定点击事件
        for (let i = 0; i < this.li.length; i++) {
            // 新函数 注意this的指向是事件源 而非Tab,需要获取外部的this
            this.li[i].onclick = function() {
                // 给点击的li绑定序号
                that.li[i].xuhao = i;
                // 点击之后隐藏上一个 获取点击时的索引(同时)
                that.hide();
                // 把被点击的li 传给getIndex形参
                that.getIndex(this);
            }
        }
    }
    Tab.prototype.hide = function() {
        // 点击后隐藏上一个
        // 取消按钮样式 隐藏页面
        this.li[this.Index].className = "";
        this.div[this.Index].style.display = "none";
    }
    Tab.prototype.getIndex = function(now) {
        // 获得点击事件的索引
        // now接收被点击的li  点击的li的序号 赋值给Index
        this.Index = now.xuhao;
        // 索引之后执行?显示
        this.show();
    }
    Tab.prototype.show = function() {
        // 根据点击按钮的索引 显示页面
        this.li[this.Index].className = "active";
        this.div[this.Index].style.display = "block";
    }
  new Tab();

============以下为非面向对象写法==========

思路:

(1)获取导航栏和页面结构 (注意是全部选中)

(2)给导航的按钮(li)手动添加键值对 用于索引

(3)遍历对象,当li点下时执行:(prev上一个 this当前)

        取消上一个按钮的颜色,隐藏上一个页面;

        设置下一个按钮的颜色,显示下一个页面;

        将当前点击的设置为下一个的上一个

(1)HTML部分

   <div class="box">
        <ul>
            <li style="background:red">热门推荐</li>
            <li>限时抢购</li>
            <li>积分兑换</li>
            <li>满减返现</li>
        </ul>
        <div class="cont">
            <div class="cont1">页面结构1</div>
            <div class="cont2"> 页面结构2</div>
            <div class="cont3">页面结构3</div>
            <div class="cont4">页面结构4</div>
        </div>
    </div>

(2)CSS部分

      .box {
            width: 400px;
            height: 300px;
            border: solid 1px black;
            margin: 0 auto;
        }
        
        .box ul {
            margin: 0;
            padding: 0;
            list-style: none;
            display: flex;
            height: 40px;
            line-height: 40px;
            text-align: center;
            border-bottom: solid 1px black
        }
        
        .box li {
            flex: 1;
            border-left: solid 1px black;
            border-right: solid 1px black;
            cursor: pointer;
        }
        
        .box li:active {
            background: #99f
        }
        
        .cont div {
            height: 259px;
            display: none;
        }
        
        .cont .cont1 {
            background: #66f;
            display: block
        }
        
        .cont .cont2 {
            background: #6f6;
        }
        
        .cont .cont3 {
            background: #f66;
        }
        
        .cont .cont4 {
            background: #ff6;
        }

(3)JS部分 

    var ali = document.querySelectorAll(".box ul li");
    var acont = document.querySelectorAll(".box .cont>div");

    for (var i = 0; i < ali.length; i++) {
        ali[i].a = i;
    }
    // console.log(ali[0].a);//检测是否添加上索引号

    // 要清除的上一个元素的索引
    var prev = 0;

    for (var i = 0; i < ali.length; i++) { //i=0
        ali[i].onclick = function() { // ali[0].onclick
            // 取消上一个按钮的样式
            ali[prev].style.background = "none";
            // 隐藏上一个页面内容
            acont[prev].style.display = "none";
            // 显示当前按钮样式 this就是当前点击的li
            this.style.background = "red";
            // 显示当前页面
            acont[this.a].style.display = "block";
            // 将当前点击的,设置为下一个的上一个
            prev = this.a;
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<div id="root"> <div id="tuijian" class="container"> <ul id="tabHead"> <li v-for="(item,index) in tabList" :key="index" :="" class="current==index?'checked'" @click="current=index"> <h4>{item.tabHead}}</h4><span>{{item.tabHeadInfo}}</span> </li> </ul> <ul id="tabBody"> <li v-for="(product,index)in productList1" :v-if="current==0"> <a href="" :title="product.title"> <img :src="product.img" :alt="product.title"> <h4><span :class="tag">{{product.tag}}</span>{{(product.title)}}</h4> <p>{{product.price | pricefmt2('¥')}}</p> </a> </li> <li v-for="(product,index)in productList2" :v-if="current==1"> <a href="" :title="product.title"> <img :src="product.img" :alt="product.title"> <h4><span :class="tag">{{product.tag}}</span>{{(product.title)}}</h4> <p>{{product.price}}</p> </a> </li> <li v-for="(product,index)in productList3" :v-if="current==2"> <a href="" :title="product.title"> <img :src="product.img" :alt="product.title"> <h4><span :class="tag">{{product.tag}}</span>{{(product.title)}}</h4> <p>{{product.price}}</p> </a> </li> <li v-for="(product,index)in productList4" :v-if="current==3"> <a href="" :title="product.title"> <img :src="product.img" :alt="product.title"> <h4><span :class="tag">{{product.tag}}</span>{{(product.title)}}</h4> <p>{{product.price}}</p> </a> </li> <li v-for="(product,index)in productList5" :v-if="current==4"> <a href="" :title="product.title"> <img :src="product.img" :alt="product.title"> <h4><span :class="tag">{{product.tag}}</span>{{(product.title)}}</h4> <p>{{product.price}}</p> </a> </li> <li v-for="(product,index)in productList6" :v-if="current==5"> <a href="" :title="product.title"> <img :src="product.img" :alt="product.title"> <h4><span :class="tag">{{product.tag}}</span>{{(product.title)}}</h4> <p>{{product.price | pricefmt2('¥')}}</p> </a> </li> </ul> </div> </div>
03-22

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值