亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

過濾 HTML 內容?

過濾 HTML 內容?

Cats萌萌 2023-09-25 16:21:00
我目前正在開發一個網頁,我希望根據按鈕按下情況來過濾圖像。因此,如果我按“假期”,它應該只顯示分配有 css 類“假期”的圖像等。我已經嘗試過以下兩種方法,但沒有讓它們發揮作用。由于對 javascript 缺乏良好的理解,可能是我這邊的一個錯誤。如何通過 W3Schools過濾 Div 元素從Stack Overflow 帖子的下拉菜單中進行過濾。* {    box-sizing: border-box;}.col-1 {    width: 8.33%;}.col-2 {    width: 16.66%;}.col-3 {    width: 25%;}.col-4 {    width: 33.33%;}.col-5 {    width: 41.66%;}.col-6 {    width: 50%;}.col-7 {    width: 58.33%;}.col-8 {    width: 66.66%;}.col-9 {    width: 75%;}.col-10 {    width: 83.33%;}.col-11 {    width: 91.66%;}.col-12 {    width: 100%;}[class*="col-"] {    float: left;    padding: 15px;    border: 1px solid red; /* Just for Display purposes */}.row::after {    content: "";    clear: both;    display: table;}.button-container {  text-align: center;}.flex-container {    display: flex;    flex-direction: row;    flex-wrap: wrap;    align-items: flex-start;}.flex-content {    width: 20%;    padding: 5px;}
查看完整描述

1 回答

?
慕標5832272

TA貢獻1966條經驗 獲得超4個贊

給你一個解決方案


filterSelection("all")

function filterSelection(c) {

  var eles = document.getElementsByClassName("flex-content");

  

  for(var i=0; i < eles.length; i++) {

    if (c === "all" || eles[i].classList.contains(c)) {

      eles[i].classList.remove("displayNone");

    } else {

      eles[i].classList.add("displayNone");

    }

  }

}

* {

    box-sizing: border-box;

}


.col-1 {

    width: 8.33%;

}


.col-2 {

    width: 16.66%;

}


.col-3 {

    width: 25%;

}


.col-4 {

    width: 33.33%;

}


.col-5 {

    width: 41.66%;

}


.col-6 {

    width: 50%;

}


.col-7 {

    width: 58.33%;

}


.col-8 {

    width: 66.66%;

}


.col-9 {

    width: 75%;

}


.col-10 {

    width: 83.33%;

}


.col-11 {

    width: 91.66%;

}


.col-12 {

    width: 100%;

}


[class*="col-"] {

    float: left;

    padding: 15px;

    border: 1px solid red; /* Just for Display purposes */

}


.row::after {

    content: "";

    clear: both;

    display: table;

}


.button-container {

  text-align: center;

}


.flex-container {

    display: flex;

    flex-direction: row;

    flex-wrap: wrap;

    align-items: flex-start;

}


.flex-content {

    width: 20%;

    padding: 5px;

}


.displayNone {

  display: none;

}

<body>

  <div class="row">

    <div class="col-12">

      <h1 style="text-align: center;">Image List</h1>

      <div class="button-container">

        <button class="btn" onclick="filterSelection('all')" >All</button>

        <button class="btn" onclick="filterSelection('holiday')" >Holidays</button>

        <button class="btn" onclick="filterSelection('work')" >Work</button>

      </div>

    </div>

    </div>

      <div class="row">

        <div class="col-2"></div>

        <div class="col-8">

          <div class="flex-container">

            <div class="flex-content">

              <img src="https://via.placeholder.com/300" style="width: 100%;"/>

            </div>

            <div class="flex-content holiday">

              <img src="https://via.placeholder.com/300" style="width: 100%;"/>

            </div>

            <div class="flex-content work">

              <img src="https://via.placeholder.com/300" style="width: 100%;"/>

            </div>

            <div class="flex-content">

              <img src="https://via.placeholder.com/300" style="width: 100%;"/>

            </div>

            <div class="flex-content">

              <img src="https://via.placeholder.com/300" style="width: 100%;"/>

            </div>

            <div class="flex-content">

              <img src="https://via.placeholder.com/300" style="width: 100%;"/>

            </div>

          </div>

        </div>

      <div class="col-2"> 

    </div>

  </div>

</body>

向按鈕添加 onClick 方法filterSelection并將值作為參數傳遞。

創建一個displayNone用于隱藏元素的類。

使用jsfiddle的解決方案


查看完整回答
反對 回復 2023-09-25
  • 1 回答
  • 0 關注
  • 131 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號