gpt4 book ai didi

javascript - 过滤 HTML 内容?

转载 作者:行者123 更新时间:2023-12-04 01:44:53 25 4
gpt4 key购买 nike

我目前正在制作一个网页,我希望根据按钮的按下来过滤图像。因此,如果我按假期,它应该只显示分配给它们的 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;
}
<body>
<div class="row">
<div class="col-12">
<h1 style="text-align: center;">Image List</h1>
<div class="button-container">
<button class="button" >All</button>
<button class="button" >Holidays</button>
<button class="button" >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>

也在 jsfiddle

最佳答案

给你一个解决方案

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 来隐藏元素。

Solution using jsfiddle

关于javascript - 过滤 HTML 内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60770812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com