[英]With multiple buttons toggle hide/show multiple images
I have tree buttons, and a lot of pictures. 我有树形按钮,还有很多图片。 I want if I click on Drums, then show me the images with drums and so on.
我想要单击鼓,然后向我展示带鼓的图像等等。 I tried a lot of codes, but none of them worked.
我尝试了很多代码,但是没有一个起作用。 What is the problem???
问题是什么???
This is my HTML: 这是我的HTML:
function ImgToggle(id) { document.getElementById('img').style.display = 'none'; document.getElementById(id).style.display = 'visible'; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button onclick="ImgToggle('drum')" style="margin: 75px 0 0 500px" class="myButton">Drums</button> <br> <button onclick="ImgToggle('piano')" style="margin-left:1000px" class="myButton">Pianos</button> <br> <button onclick="ImgToggle('guitar')" style="margin-left:175px" class="myButton">Guitars</button> <br> <img id="guitar" src="Style/Image/Fender1.jpg" style="width:100px;height:100px;padding-left:35px;"></img> <img id="guitar" src="Style/Image/Fender2.jpg" style="width:100px;height:100px;padding-left:35px;"></img> <img id="guitar" src="Style/Image/Gibson.jpg" style="width:100px;height:100px;padding-left:35px;"></img> <img id="guitar" src="Style/Image/Epiphone1.jpg" style="width:100px;height:100px;padding-left:35px;"></img> <img id="guitar" src="Style/Image/Epiphone2.jpg" style="width:100px;height:100px;padding-left:35px;"></img> <img id="piano" src="Style/Image/Casio.jpg" style="width:100px;height:100px;padding-left:35px;"></img> <img id="piano" src="Style/Image/Roland.jpg" style="width:100px;height:100px;padding-left:35px;"></img> <img id="piano" src="Style/Image/Roland2.jpg" style="width:100px;height:100px;padding-left:35px;"></img> <img id="drum" src="Style/Image/LudwigBasic.jpg" style="width:100px;height:100px;padding-left:35px;"></img> <img id="drum" src="Style/Image/LudwigFull.jpg" style="width:100px;height:100px;padding-left:35px;"></img> <img id="drum" src="Style/Image/PearlBasic.jpg" style="width:100px;height:100px;padding-left:35px;"></img>
HTML does not allow duplicate IDs. HTML不允许重复的ID。 Use classes to select the items instead.
使用类来选择项目。
Most browsers will only return the first match as IDs are stored in a fast lookup dictionary (eg with only one element per ID). 由于ID存储在快速查找字典中(例如,每个ID仅包含一个元素),因此大多数浏览器只会返回第一个匹配项。
You did tag the question with jQuery
so you may want to use something like this: https://jsfiddle.net/TrueBlueAussie/7c8zapnw/ 您确实使用
jQuery
标记了问题,所以您可能想要使用类似以下的内容: https : //jsfiddle.net/TrueBlueAussie/7c8zapnw/
$('.myButton').click(function(){
var $target = $($(this).attr("target"));
$('img').hide();
$target.show();
});
And the HTML like this: 和这样的HTML:
<button target=".drum" style="margin: 75px 0 0 500px" class="myButton">Drums</button>
<br>
<button target=".piano" style="margin-left:1000px" class="myButton">Pianos</button>
<br>
<button target=".guitar" style="margin-left:175px" class="myButton">Guitars</button>
<br>
<img class="guitar" src="Style/Image/Fender1.jpg" style="width:100px;height:100px;padding-left:35px;"/>
<img class="guitar" src="Style/Image/Fender2.jpg" style="width:100px;height:100px;padding-left:35px;"/>
<img class="guitar" src="Style/Image/Gibson.jpg" style="width:100px;height:100px;padding-left:35px;"/>
<img class="guitar" src="Style/Image/Epiphone1.jpg" style="width:100px;height:100px;padding-left:35px;"/>
<img class="guitar" src="Style/Image/Epiphone2.jpg" style="width:100px;height:100px;padding-left:35px;"/>
<img class="piano" src="Style/Image/Casio.jpg" style="width:100px;height:100px;padding-left:35px;"/>
<img class="piano" src="Style/Image/Roland.jpg" style="width:100px;height:100px;padding-left:35px;"/>
<img class="piano" src="Style/Image/Roland2.jpg" style="width:100px;height:100px;padding-left:35px;"/>
<img class="drum" src="Style/Image/LudwigBasic.jpg" style="width:100px;height:100px;padding-left:35px;"/>
<img class="drum" src="Style/Image/LudwigFull.jpg" style="width:100px;height:100px;padding-left:35px;"/>
<img class="drum" src="Style/Image/PearlBasic.jpg" style="width:100px;height:100px;padding-left:35px;"/>
img
which does not exist. img
标识获取img元素。 </img>
</img>
jQuery/Javascript code has to follow the DOM elements it references (before the end of the body
element), or (more usually) be wrapped in a DOM ready handler like this: jQuery / Javascript代码必须遵循它引用的DOM元素(在
body
元素的末尾之前),或者(通常)包装在DOM就绪处理程序中,如下所示:
$(document).ready(function(){
// Your code here
});
A shorter/smarter DOM ready handler is 较短/更智能的DOM ready处理程序是
$(function(){
// Your code here
});
I say smarter as it support this version: 我说的更好,因为它支持此版本:
jQuery(function($){
// Your code here using a locally scoped $!
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.