使用单选按钮显示特定图像
Using radio buttons to display particular images
我使用 echo 来显示和添加两个不同目录(一个包含连帽衫,一个包含 T 恤)中所有图像的 class 名称。我想要单选按钮,单击时仅显示特定的 class(例如,如果您单击 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 时不起作用)。我怎样才能让它工作?我怎样才能写得更好(所以它只显示一个 class,而不是不显示另一个)。
我也不知道如何让所有按钮工作。
#teeshirts:checked ~ .teeshirt {
display: none;
}
#hoodies:checked ~ .hoodie {
display: none;
}
这可能是一个使用原始数据的快速解决方案,您可以轻松地使用 PHP
HTML代码
<div>
<label><input type="radio" name="colorRadio" value="red"> red</label>
<label><input type="radio" name="colorRadio" value="green"> green</label>
<label><input type="radio" name="colorRadio" value="blue"> blue</label>
</div>
<div class="red box">You have selected <strong>red radio button</strong> so i am here</div>
<div class="green box">You have selected <strong>green radio button</strong> so i am here</div>
<div class="blue box">You have selected <strong>blue radio button</strong> so i am here</div>
CSS代码
<style type="text/css">
.box{
padding: 20px;
display: none;
margin-top: 20px;
border: 1px solid #000;
}
.red{ background: #ff0000; }
.green{ background: #00ff00; }
.blue{ background: #0000ff; }
</style>
也试试这个脚本
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('input[type="radio"]').click(function(){
if($(this).attr("value")=="red"){
$(".box").not(".red").hide();
$(".red").show();
}
if($(this).attr("value")=="green"){
$(".box").not(".green").hide();
$(".green").show();
}
if($(this).attr("value")=="blue"){
$(".box").not(".blue").hide();
$(".blue").show();
}
});
});
</script>
我使用 echo 来显示和添加两个不同目录(一个包含连帽衫,一个包含 T 恤)中所有图像的 class 名称。我想要单选按钮,单击时仅显示特定的 class(例如,如果您单击 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 时不起作用)。我怎样才能让它工作?我怎样才能写得更好(所以它只显示一个 class,而不是不显示另一个)。
我也不知道如何让所有按钮工作。
#teeshirts:checked ~ .teeshirt {
display: none;
}
#hoodies:checked ~ .hoodie {
display: none;
}
这可能是一个使用原始数据的快速解决方案,您可以轻松地使用 PHP
HTML代码
<div>
<label><input type="radio" name="colorRadio" value="red"> red</label>
<label><input type="radio" name="colorRadio" value="green"> green</label>
<label><input type="radio" name="colorRadio" value="blue"> blue</label>
</div>
<div class="red box">You have selected <strong>red radio button</strong> so i am here</div>
<div class="green box">You have selected <strong>green radio button</strong> so i am here</div>
<div class="blue box">You have selected <strong>blue radio button</strong> so i am here</div>
CSS代码
<style type="text/css">
.box{
padding: 20px;
display: none;
margin-top: 20px;
border: 1px solid #000;
}
.red{ background: #ff0000; }
.green{ background: #00ff00; }
.blue{ background: #0000ff; }
</style>
也试试这个脚本
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('input[type="radio"]').click(function(){
if($(this).attr("value")=="red"){
$(".box").not(".red").hide();
$(".red").show();
}
if($(this).attr("value")=="green"){
$(".box").not(".green").hide();
$(".green").show();
}
if($(this).attr("value")=="blue"){
$(".box").not(".blue").hide();
$(".blue").show();
}
});
});
</script>