简体   繁体   English

一次仅一个Active Class和Visible Div

[英]Only one Active class and Visible Div at a time

There are a bunch of div elements on the page. 页面上有一堆div元素。

I have a nested div inside of them. 我里面有一个嵌套的div

I want to be able to add a class to the click ed element, and .show() the child div. 我希望能够向click ed元素添加一个类, .show()子div添加.show()

$('.container').on('click', function(){
    $(this).toggleClass('red').children('.insideItem').slideToggle();
});
  • I can click on it, it drops down. 我可以单击它,它会下降。
  • Click again, it goes away. 再次单击,它消失了。

So, now I need some method to removeClass() and slideUp() all of the other ones in the event of a click anywhere except the open div . 因此,现在我需要一些方法,以在除open div以外的任何位置单击时,将slideUp() removeClass()slideUp() 所有其他方法slideUp() Naturally, I tried something like this: 自然,我尝试过这样的事情:

$('html').on('click', function(){
    $('.container').removeClass('red').children('div').slideUp();
});

Well, that just stops the effect from staying in the first place. 好吧,这只是阻止效果停留在首位。 I've read around on event.Propagation() but I've read that should be avoided if possible. 我已经阅读了有关event.Propagation()但我已阅读,如果可能的话,应避免使用。

I'm trying to avoid using any more prebuilt plugins like accordion , as this should be a pretty straightforward thing to accomplish and I'd like to know a simple way to make it work. 我试图避免使用更多的预构建插件(例如accordion ,因为这应该是一件非常简单的事情,而且我想知道一种使其工作的简单方法。

Would anyone be able to show a quick example on this fiddle how to resolve this? 谁能在这个小提琴上展示一个简单的例子,如何解决这个问题?

  • Show only one active div, and collapse all others if clicked off 仅显示一个活动div,如果单击则折叠所有其他div

https://jsfiddle.net/4x1Lsryp/ https://jsfiddle.net/4x1Lsryp/

One way to go about it is to update your code with the following: 一种解决方法是使用以下代码更新代码:

1) prevent the click on a square from bubbling up to the parent elements 2) make sure to reset the status of all the squares when a new click is made anywhere. 1)防止对正方形的点击冒泡至父元素2)确保在任何地方进行新点击时重置所有正方形的状态。

$('.container').on('click', function(){
  $this = $(this);
  $('.container').not($this).removeClass('red').children('div').slideUp();
  $this.toggleClass('red').children('div').slideToggle();
  return false;
});

See the updated JSfiddle here: https://jsfiddle.net/pdL0y0xz/ 在此处查看更新的JSfiddle: https ://jsfiddle.net/pdL0y0xz/

You need to combine your two approaches: 您需要结合两种方法:

 for (var i = 0; i < 10; i++) { $('#wrap').append("<div class='container'>" + i + "<div class='insideDiv'>Inside Stuff</div></div>"); } $('.container').on('click', function() { var hadClassRed = $(this).hasClass('red'); $('.container').removeClass('red').children('div').slideUp(); if (!hadClassRed) { $(this).toggleClass('red').children('div').slideToggle(); } }); 
 .container { width: 200px; height: 200px; float: left; background: gray; margin: 1em; } .insideDiv { display: none; } .red { background: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="wrap"></div> 

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

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