简体   繁体   English

隐藏显示不适用于多个div

[英]hide show not working on multiple divs

I am using the slideToggle method to hide/show a div. 我正在使用slideToggle方法隐藏/显示div。 I am trying to replicate this action multiple times on a page but it only works on the 1st one. 我试图在一个页面上多次复制此操作,但仅在第一个页面上有效。 I figured out a way to have it work on each div but have to rename each div and add a new function in the javascript. 我想出了一种方法可以在每个div上工作,但必须重命名每个div并在javascript中添加新功能。 What can I do to clean up my code as I am going to be using this through out the page and be creating new instances for each div does is not efficient. 我将要在整个页面中使用该代码并为每个div创建新实例,这对我清理代码是无效的。

<script>
  $(document).ready(function() {
    $("#flip").click(function() {
      $("#panel").slideToggle("slow");
    });
    $("#flip2").click(function() {
      $("#panel2").slideToggle("slow");
    });
    $("#flip3").click(function() {
      $("#panel3").slideToggle("slow");
    });
  });

</script>

<p class="button-label">Active</p>
<button type="button" class="btn a-cart"><span>ADD TO CART</span></button>
<div id="flip">Click to view HTML</div>
<div id="panel" style="display:none">
  <textarea rows="3" cols="50">
    <button type="button" class="btn a-cart"><span>ADD TO CART</span></button>
  </textarea>
</div>

<p class="button-label">Disabled</p>
<button type="button" class="btn a-cart disabled"><span>ADD TO CART</span></button>
<div id="flip2">Click to view HTML</div>
<div id="panel2" style="display:none">
  <textarea rows="3" cols="50">
    <button type="button" class="btn a-cart disabled"><span>ADD TO CART</span></button>
  </textarea>
</div>

<p class="button-label">Select Options</p>
<button type="button" class="btn select-options"><span>select options</span></button>
<div id="flip3">Click to view HTML</div>
<div id="panel3" style="display:none">
  <textarea rows="3" cols="50">
    <button type="button" class="btn select-options"><span>select options</span></button>
  </textarea>
</div>

You can use classes instead of ids and use .next() method in callback function: 您可以使用类而不是id并在回调函数中使用.next()方法:

 $(document).ready(function(){ $(".flip").click(function(){ $(this).next('.panel').slideToggle("slow"); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p class="button-label">Active</p> <button type="button" class="btn a-cart"><span>ADD TO CART</span></button> <div class="flip">Click to view HTML</div> <div class="panel" style="display:none"> <textarea rows="3" cols="50"><button type="button" class="btn a-cart"><span>ADD TO CART</span></button> </textarea> </div> <p class="button-label">Disabled</p> <button type="button" class="btn a-cart disabled"><span>ADD TO CART</span></button> <div class="flip">Click to view HTML</div> <div class="panel" style="display:none"> <textarea rows="3" cols="50"><button type="button" class="btn a-cart disabled"><span>ADD TO CART</span> </button> </textarea> </div> <p class="button-label">Select Options</p> <button type="button" class="btn select-options"><span>select options</span></button> <div class="flip">Click to view HTML</div> <div class="panel" style="display:none"> <textarea rows="3" cols="50"><button type="button" class="btn select-options"><span>select options</span> </button> </textarea> </div> 

You need not to replicate this and rename <div> and create new function. 您无需复制它并重命名<div>并创建新功能。

Take these steps: 请执行以下步骤:

  1. Give same class to each div where you click to perform slideToggle(eg. flip). 将相同的类分配给您单击以执行slideToggle(例如flip)的每个div。
  2. Use selector $(this) to identify clicked element. 使用选择器$(this)标识单击的元素。
  3. Use jquery .next() function to get the .next element. 使用jquery .next()函数获取.next元素。
  4. Call .slideToggle() on that. 调用.slideToggle()
  5. It's Done!! 完成!!

See the code below: 请参见下面的代码:

    <script>
        $(document).ready(function() {
           $(".flip").click(function() {
              $(this).next().slideToggle("slow");
           });
        });

    </script>

    <p class="button-label">Active</p>
    <button type="button" class="btn a-cart"><span>ADD TO CART</span></button>
    <div class="flip">Click to view HTML</div>
    <div id="panel" style="display:none">
        <textarea rows="3" cols="50">
          <button type="button" class="btn a-cart"><span>ADD TO CART</span>  </button>
        </textarea>
    </div>

    <p class="button-label">Disabled</p>
    <button type="button" class="btn a-cart disabled"><span>ADD TO CART</span></button>
    <div class="flip">Click to view HTML</div>
    <div id="panel2" style="display:none">
      <textarea rows="3" cols="50">
        <button type="button" class="btn a-cart disabled"><span>ADD TO  CART</span></button>
      </textarea>
    </div>

    <p class="button-label">Select Options</p>
      <button type="button" class="btn select-options"><span>select    options</span></button>
    <div class="flip">Click to view HTML</div>
    <div id="panel3" style="display:none">
        <textarea rows="3" cols="50">
          <button type="button" class="btn select-options"><span>select options</span></button>
        </textarea>
    </div>

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

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