【发布时间】:2016-03-30 17:54:08
【问题描述】:
我使用 echo 在两个不同的目录(一个包含连帽衫,一个包含 T 恤)中显示和添加所有图像的类名。我想要单选按钮,当点击时,只显示特定的类(例如,如果你点击 teeshirts,它只显示 teeshirts。
<input id="all" type="radio" name="category"> <label for="all">All</label> <br/>
<input id="hoodies" type="radio" name="category"/> <label for="hoodies">Hoodies</label> <br/>
<input id="teeshirts" type="radio" name="category"> <label for="teeshirts">Teeshirts</label> <br/>
<!-- Images -->
<?php
// Teeshirts
$dir = 'images/clothing/hoodies/small/';
$files = scandir($dir);
$images = array();
foreach($files as $file) {
if(fnmatch('*.png',$file)) {
$images[] = $file;
}
if(fnmatch('*.jpg',$file)) {
$images[] = $file;
}
}
foreach($images as $image) {
echo '<img src="images/clothing/hoodies/small/'.$image.'"width="15%" height="20%" hspace="2%" vspace="2%" class="hoodie" data-big="images/clothing/hoodies/large/'.$image.'" />';
// echo '<p>'.$image.'</p>';
}
// Hoodies
$dir = 'images/clothing/teeshirts/small/';
$files = scandir($dir);
$images = array();
foreach($files as $file) {
if(fnmatch('*.png',$file)) {
$images[] = $file;
}
if(fnmatch('*.jpg',$file)) {
$images[] = $file;
}
}
foreach($images as $image) {
echo '<img src="images/clothing/teeshirts/small/'.$image.'" width="15%" height="20%" hspace="2%" vspace="2%" class="teeshirt" data-big="images/clothing/teeshirts/large/'.$image.'" />';
}
?>
这是我目前的 CSS
#teeshirts:checked ~ .hoodie {
display: none;
}
#hoodies:checked ~ .teeshirt {
display: none;
}
它现在的工作方式是说,如果您点击 T 恤,则不要显示连帽衫(只显示 T 恤)。但是现在这根本行不通(以前我对每个图像进行硬编码时使用它,但现在我使用 php 时就不行了)。我怎样才能让它工作?我怎样才能写得更好(所以它只显示一个类,而不是不显示另一个)。
我也不知道如何让所有按钮工作。
【问题讨论】:
-
重要的是用户在选择单选按钮时无法访问图像还是只是一个隐藏图像就足够了,因为我看到了您的 PHP 标签,但在这种情况下不需要 PHP
-
我不太明白你的意思?我使用 php,这样我就可以一次添加整个目录,而不必为每个单独的图像做一百万次 。