简体   繁体   English

如何在img上单击,仅显示一个并隐藏其他div

[英]How to on img click, show just one, and hide other divs

I'm working on project, and got stuck with JS. 我正在从事项目,并且陷入了JS的困境。 Please before you read text, see the picture. 在阅读文字之前,请先查看图片。 Picture 图片

I want to make a slider (And I did), and now when I click on a circle, I want to show content-box only with the same color, and others should be hidden. 我想制作一个滑块(我确实做了),现在当我单击一个圆时,我只想显示具有相同颜色的内容框,而其他的则应隐藏。

eg when I click on .imgRed, I want only #red to be displayed, others, #blue and #black to be display: none; 例如,当我单击.imgRed时,我只希望显示#red,而其他则显示#blue和#black display: none; .

Thank you. 谢谢。

You probably know what you should do but you are not confident about it. 您可能知道应该做些什么,但对它没有信心。 You should hide others but the one that you want to stay present. 您应该隐藏其他人,但您要留下来。
For example for red you should do: 例如,对于红色,您应该这样做:

$('.imgRed').click(function() {
    $('#red').show();
    $('#black').hide();
    $('#blue').hide();
}

and repeat for all of the others. 并重复其他所有步骤。

I tried to create a working snippet according to the image, where I used: 我尝试根据使用的图片创建一个有效的代码段:

  • A class for the circles, another class for the bars, 圈子的一类,酒吧的一类,
  • The circles got a data-color attribute that will target the bar to be displayed on click. 圆圈具有data-color属性,该属性将定位单击时显示的条。

Here it is: 这里是:

 // When clicking a circle, use the data-colo to display the correct bar $('.circle').click(function() { $('.bar').hide(); var color = $(this).data('color'); $('#' + color).show(); }); // Trigger the click event on load: $('.imgRed').trigger('click'); 
 .circle { display: inline-block; margin: 8px 16px; border: 30px solid; border-radius: 50%; height: 0; width: 0; } .bar { display: none; margin: 8px 16px; height: 40px; width: 250px; } .imgRed { border-color: red; background-color: red; } .imgBlack { border-color: black; background-color: black; } .imgBlue { border-color: blue; background-color: blue; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="circle imgRed" data-color="red"></div> <div class="circle imgBlack" data-color="black"></div> <div class="circle imgBlue" data-color="blue"></div> <br><br> <div class="bar imgRed" id="red"></div> <div class="bar imgBlack" id="black"></div> <div class="bar imgBlue" id="blue"></div> 

Hope it helps. 希望能帮助到你。

$('.imgRed').click(function(){
    $('#red').show();
    $('#blue,#black').hide();
});

now when you click on all elements with class .imgRed only those with ID red will be visible. 现在,当您单击所有具有.imgRed类的元素时,只有那些具有ID红色的元素才可见。

the same you have to do with your other classes; 与其他课程相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM