过滤 HTML 内容?

我目前正在开发一个网页,我希望根据按钮按下情况来过滤图像。因此,如果我按“假期”,它应该只显示分配有 css 类“假期”的图像等。

我已经尝试过以下两种方法,但没有让它们发挥作用。由于对 javascript 缺乏良好的理解,可能是我这边的一个错误。

* {

    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;

}


Cats萌萌
浏览 103回答 1
1回答

慕标5832272

给你一个解决方案filterSelection("all")function filterSelection(c) {&nbsp; var eles = document.getElementsByClassName("flex-content");&nbsp;&nbsp;&nbsp; for(var i=0; i < eles.length; i++) {&nbsp; &nbsp; if (c === "all" || eles[i].classList.contains(c)) {&nbsp; &nbsp; &nbsp; eles[i].classList.remove("displayNone");&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; eles[i].classList.add("displayNone");&nbsp; &nbsp; }&nbsp; }}* {&nbsp; &nbsp; box-sizing: border-box;}.col-1 {&nbsp; &nbsp; width: 8.33%;}.col-2 {&nbsp; &nbsp; width: 16.66%;}.col-3 {&nbsp; &nbsp; width: 25%;}.col-4 {&nbsp; &nbsp; width: 33.33%;}.col-5 {&nbsp; &nbsp; width: 41.66%;}.col-6 {&nbsp; &nbsp; width: 50%;}.col-7 {&nbsp; &nbsp; width: 58.33%;}.col-8 {&nbsp; &nbsp; width: 66.66%;}.col-9 {&nbsp; &nbsp; width: 75%;}.col-10 {&nbsp; &nbsp; width: 83.33%;}.col-11 {&nbsp; &nbsp; width: 91.66%;}.col-12 {&nbsp; &nbsp; width: 100%;}[class*="col-"] {&nbsp; &nbsp; float: left;&nbsp; &nbsp; padding: 15px;&nbsp; &nbsp; border: 1px solid red; /* Just for Display purposes */}.row::after {&nbsp; &nbsp; content: "";&nbsp; &nbsp; clear: both;&nbsp; &nbsp; display: table;}.button-container {&nbsp; text-align: center;}.flex-container {&nbsp; &nbsp; display: flex;&nbsp; &nbsp; flex-direction: row;&nbsp; &nbsp; flex-wrap: wrap;&nbsp; &nbsp; align-items: flex-start;}.flex-content {&nbsp; &nbsp; width: 20%;&nbsp; &nbsp; padding: 5px;}.displayNone {&nbsp; display: none;}<body>&nbsp; <div class="row">&nbsp; &nbsp; <div class="col-12">&nbsp; &nbsp; &nbsp; <h1 style="text-align: center;">Image List</h1>&nbsp; &nbsp; &nbsp; <div class="button-container">&nbsp; &nbsp; &nbsp; &nbsp; <button class="btn" onclick="filterSelection('all')" >All</button>&nbsp; &nbsp; &nbsp; &nbsp; <button class="btn" onclick="filterSelection('holiday')" >Holidays</button>&nbsp; &nbsp; &nbsp; &nbsp; <button class="btn" onclick="filterSelection('work')" >Work</button>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <div class="row">&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-2"></div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-8">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="flex-container">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="flex-content">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="https://via.placeholder.com/300" style="width: 100%;"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="flex-content holiday">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="https://via.placeholder.com/300" style="width: 100%;"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="flex-content work">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="https://via.placeholder.com/300" style="width: 100%;"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="flex-content">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="https://via.placeholder.com/300" style="width: 100%;"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="flex-content">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="https://via.placeholder.com/300" style="width: 100%;"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="flex-content">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="https://via.placeholder.com/300" style="width: 100%;"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <div class="col-2">&nbsp;&nbsp; &nbsp; </div>&nbsp; </div></body>向按钮添加 onClick 方法filterSelection并将值作为参数传递。创建一个displayNone用于隐藏元素的类。使用jsfiddle的解决方案
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5