简体   繁体   English

jquery 如何一次只有 1 个活动切换?

[英]jquery how to only have 1 active toggle at a time?

I'm making a website that requires me to have 18 icons of which, each icon has its own assign div that is hidden, and once pressed the hidden Div slides down and shows below the icon.我正在制作一个需要我有 18 个图标的网站,每个图标都有自己的隐藏分配 div,一旦按下隐藏的 div 就会向下滑动并显示在图标下方。 These icons have to be in a different Div from the content I want to hide/show.这些图标必须与我要隐藏/显示的内容位于不同的 Div 中。

I'm using Elementor on wordpress since I'm really ignorant when it comes to webdesign and programing,我在 wordpress 上使用 Elementor,因为我对网页设计和编程一无所知,

I've found this jquery that I'm using to show and hide Div when I click on icons.我找到了这个 jquery,当我点击图标时,我用它来显示和隐藏 Div。 I've assign the icons as.showBlock1.ShowBlock2.ShowBlock3 etc and the Divs as.hiddenBlock1.hiddenBlock2.hiddenBlock3 etc... and it works how I want except that I only want 1 active at a time, so that if I press icon1, it shows Div1 and then if I press icon2, it hides Div1 and shows Div2 and so on.我已将图标分配为.showBlock1.ShowBlock2.ShowBlock3 等,并将 Divs 分配为.hiddenBlock1.hiddenBlock2.hiddenBlock3 等......它按我想要的方式工作,除了我一次只想要 1 个活动,所以如果我按下icon1,它显示 Div1,然后如果我按 icon2,它会隐藏 Div1 并显示 Div2,依此类推。

jQuery(document).ready(function( $ ){
    
  var hbtn = $(".showBlock");
  var hcon = $(".hiddenBlock");
  
   hcon.hide();
   hbtn.click(function(e) {
   var index = hbtn.index(this) 
   $(hcon).eq(index).slideToggle("slow");

   e.preventDefault();     
    });
});

jQuery(document).ready(function( $ ){
    
  var hbtn = $(".showBlock2");
  var hcon = $(".hiddenBlock2");
  
   hcon.hide();
   hbtn.click(function(e) {
   var index = hbtn.index(this) 
   $(hcon).eq(index).slideToggle("slow");

   e.preventDefault();     
    });
});

Since I'm really ignorant to coding I've been just repeating the script and changing the numbers on the class.showBlock and.hiddenBlock.由于我对编码一无所知,我一直在重复脚本并更改 class.showBlock 和.hiddenBlock 上的数字。

Maybe the following is helpful to you?也许以下内容对您有帮助?

 $(function(){ var hbtn = $(".showBlock"); var hcon = $(".hiddenBlock"); hcon.hide(); hbtn.click(function(e) { var curr=hcon.eq(hbtn.index(this)); hcon.not(curr).hide(); curr.slideToggle("slow"); e.preventDefault(); }); });
 .cont1 {height:50px}.hiddenBlock {display: inline-block; width:60px; height: 40px; background-color:#ccc}
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="cont1"> <div class="hiddenBlock">1</div> <div class="hiddenBlock">2</div> <div class="hiddenBlock">3</div> <div class="hiddenBlock">4</div> <div class="hiddenBlock">5</div> <div class="hiddenBlock">6</div> </div> <div> <button class="showBlock">show 1</button> <button class="showBlock">show 2</button> <button class="showBlock">show 3</button> <button class="showBlock">show 4</button> <button class="showBlock">show 5</button> <button class="showBlock">show 6</button> </div>

A button click will at first hide all visile .hcon elements and will then .slideToggle the one corresponding to the button position.单击按钮将首先隐藏所有可见的.hcon元素,然后将.slideToggle与按钮 position 对应的元素。

Edit编辑

After having had a look at your web page I can simplify your structure as shown below:在查看了您的 web 页面后,我可以简化您的结构,如下所示:

 jQuery(function($){ const btns=$("div.showBlock"); const scts=$("section.hiddenBlock"); $("section").on("click","div.showBlock",function(){ let curr=scts.eq(btns.index(this)); scts.not(curr).hide(); curr.toggle(); }); })
 .showBlock {display: inline-block; background-color:#ccc; width:50px; margin:2px;padding:6px}.hiddenBlock {background-color:#eea; width:192px; padding:6px; margin-top:8px}
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>... <section class="... elementor-element-51eaf85..." data-id="51eaf85" data-element_type="section"> <div class="elementor-container elementor-column-gap-no"> <div class="elementor-column... elementor-element-1a773d0 showBlock"...> one </div> <div class="elementor-column... elementor-element-6b9441a showBlock"...> two </div> <div class="elementor-column... elementor-element-ce4f2d4 showBlock"...> three </div> </div> </section> <section class="... elementor-element-fc98ed1 hiddenBlock..." data-id="fc98ed1" style="">first section</section> <section class="... elementor-element-cf64eb5 hiddenBlock..." data-id="cf64eb5" style="display: none;">second section</section> <section class="... elementor-element-f16da07 hiddenBlock..." data-id="f16da07" style="display: none;">third section</section>...

The HTML structure there is an excerpt from the page. HTML 结构有一个页面摘录。 I removed a few class attributes to improve the readability and added a little makeshift CSS to make it "presentable".我删除了一些 class 属性以提高可读性,并添加了一些临时的 CSS 以使其“像样”。

On top of that I unified your classes "showBlock1", "showBlock2", "showBlock3" to "showBlock" and "hiddenBlock", "hiddenBlock2", "hiddenBlock3" to "hiddenBlock".最重要的是,我将您的类“showBlock1”、“showBlock2”、“showBlock3”统一到“showBlock”和“hiddenBlock”、“hiddenBlock2”、“hiddenBlock3”到“hiddenBlock”。

Have a look at it and play around with it...看看它并玩弄它......

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

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