最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

原生JS實現(xiàn)Tab欄切換功能的示例代碼

 更新時間:2026年04月12日 13:49:04   作者:vmiao  
本文主要介紹了如何使用JavaScript實現(xiàn)一個簡單的商品展示頁面,包括前置知識點的講解、功能模塊的編寫以及易錯點的總結,如對象解構、事件綁定和箭頭函數(shù)的使用等,需要的朋友可以參考下

一、效果展示

圖片展示:

效果展示視頻:(忽略清朝畫質(zhì))

二、前置知識點

1.解構賦值:數(shù)組解構、對象解構
2.事件委托,利用冒泡原理為多個元素綁定事件
3.forEach()--遍歷數(shù)組,循環(huán);filter()篩選數(shù)組
4.箭頭函數(shù)

三、練習素材(html+css)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>第一天練習</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        li {
            list-style: none;
        }
        a {
            text-decoration: none;
        }
        .viewPort {
            width: 1024px;
            height: 780px;
            margin: 0 auto;
        }
        .list {
            margin-top: 30px;
            display: flex;
            width: 1024px;
            flex-wrap: wrap;
        }
        .item {
            width: 240px;
            margin-left: 10px;
            padding: 20px 30px;
            transition: all .5s;
            margin-bottom: 20px;
        }
        .item:nth-child(4n) {
            margin-left: 0;
        }
        .item:hover {
            box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.2);
            transform: translate3d(0, -4px, 0);
            cursor: pointer;
        }
        .item img {
            width: 100%;
        }
        .item .name {
            font-size: 18px;
            margin-bottom: 10px;
            color: #666;
        }
        .item .price {
            font-size: 22px;
            color: firebrick;
        }
        .item .price::before {
            content: "¥";
            font-size: 14px;
        }
        .filter {
            display: flex;
            width: 990px;
            margin: 0 auto;
            padding: 50px 30px;
        }
        .filter a {
            padding: 10px 20px;
            background: #f5f5f5;
            color: #666;
            text-decoration: none;
            margin-right: 20px;
        }
        .filter a:active,
        .filter a:focus {
            background: #05943c;
            color: #fff;
        }
    </style>
</head>
<body>
    <div class="viewPort">
        <div class="filter">
            <a data-index="1" href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >0-100元</a>
            <a data-index="2" href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >100-300元</a>
            <a data-index="3" href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >大于300元</a>
            <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >全部區(qū)間</a>
        </div>
        <div class="list">
            <!-- <div class="item">
                <img src="" alt="">
                <p class="name"></p>
                <p class="price"></p>
            </div> -->
        </div>
    </div>
    <script>
        // 2. 初始化數(shù)據(jù)
        const goodsList = [
            {
                id: '4001172',
                name: '稱心如意手搖咖啡磨豆機咖啡豆研磨機',
                price: '289.00',
                picture: 'https://yanxuan-item.nosdn.127.net/84a59ff9c58a77032564e61f716846d6.jpg',
            },
            {
                id: '4001594',
                name: '日式黑陶功夫茶組雙側把茶具禮盒裝',
                price: '288.00',
                picture: 'https://yanxuan-item.nosdn.127.net/3346b7b92f9563c7a7e24c7ead883f18.jpg',
            },
            {
                id: '4001009',
                name: '竹制干泡茶盤正方形瀝水茶臺品茶盤',
                price: '109.00',
                picture: 'https://yanxuan-item.nosdn.127.net/2d942d6bc94f1e230763e1a5a3b379e1.png',
            },
            {
                id: '4001874',
                name: '古法溫酒汝瓷酒具套裝白酒杯蓮花溫酒器',
                price: '488.00',
                picture: 'https://yanxuan-item.nosdn.127.net/44e51622800e4fceb6bee8e616da85fd.png',
            },
            {
                id: '4001649',
                name: '大師監(jiān)制龍泉青瓷茶葉罐',
                price: '139.00',
                picture: 'https://yanxuan-item.nosdn.127.net/4356c9fc150753775fe56b465314f1eb.png',
            },
            {
                id: '3997185',
                name: '與眾不同的口感汝瓷白酒杯套組1壺4杯',
                price: '108.00',
                picture: 'https://yanxuan-item.nosdn.127.net/8e21c794dfd3a4e8573273ddae50bce2.jpg',
            },
            {
                id: '3997403',
                name: '手工吹制更厚實白酒杯壺套裝6壺6杯',
                price: '99.00',
                picture: 'https://yanxuan-item.nosdn.127.net/af2371a65f60bce152a61fc22745ff3f.jpg',
            },
            {
                id: '3998274',
                name: '德國百年工藝高端水晶玻璃紅酒杯2支裝',
                price: '139.00',
                picture: 'https://yanxuan-item.nosdn.127.net/8896b897b3ec6639bbd1134d66b9715c.jpg',
            },
        ]

四、功能JS模塊

1.渲染函數(shù)的封裝

function(arr){
    let str = "" //聲明一個空字符串
    arr.forEach(item => {
        const {name,picture,price} = item //對象解構,快速批量的聲明變量,縮減代碼量
        str += `
            <div class="item">
                <img src=${picture} alt="">
                <p class="name">${name}</p>
                <p class="price">${price}</p>
            </div> 
        ` 
        //arr中有幾個元素就渲染幾個div,將數(shù)據(jù)填入 ${},動態(tài)渲染
    })
    document.queryselector(".list").innerHTML = str //將字符串放入.list的div盒子中
   }
   render(goodsList) //調(diào)用函數(shù)

2.綁定點擊事件

    //事件委托,為父元素綁定點擊事件
    document.queryselector("filter").addEventListener("click",e =>{
        const {tagName,dataset} = e.target //獲取點擊對象+對象解構
        let arr = goodsList //因為如果都不點擊就顯示全部元素,所以初始值將arr直接等于goodsList
        if(tagName === "A"){ //點擊a標簽時才觸發(fā),點擊父元素空白區(qū)域不觸發(fā)
            if (dataset.index === "1") {
                    arr = goodsList.filter(item => item.price > 0 && item.price <= 100)
                } else if (dataset.index === "2") {
                    arr = goodsList.filter(item => item.price >= 100 && item.price <= 300)
                } else if (dataset.index === "3") {
                    arr = goodsList.filter(item => item.price >= 300)
                }
                render(arr)
        }
    })

五、易錯點

1.對象解構時,例如const {name,price} = item //這里是花括號,數(shù)組解構時是[]。并且對象解構時聲名的變量名必須和數(shù)據(jù)中的屬性名一致

2.函數(shù)不調(diào)用不執(zhí)行,封裝好函數(shù)后一定要調(diào)用執(zhí)行

3.箭頭函數(shù)省略原則:參數(shù)只有一個時,()可以省略;若函數(shù)中只有一句return語句時,{}和return都可以省略;!??!沒有參數(shù)時,()一定不要省

以上就是原生JS實現(xiàn)Tab欄切換功能的示例代碼的詳細內(nèi)容,更多關于JS Tab欄切換功能的資料請關注腳本之家其它相關文章!

相關文章

最新評論

海门市| 保亭| 遂平县| 大足县| 中阳县| 德钦县| 咸丰县| 红桥区| 扶风县| 长岛县| 九江县| 怀集县| 云龙县| 陆川县| 天祝| 安乡县| 丹巴县| 台山市| 滕州市| 丹阳市| 沐川县| 文成县| 黔西县| 临海市| 思茅市| 牙克石市| 鸡泽县| 湘阴县| 扎囊县| 滕州市| 蓬安县| 喀什市| 杭锦旗| 瑞金市| 宁远县| 阿尔山市| 宁海县| 揭阳市| 宝山区| 金山区| 杭州市|