最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How to fix this simple accordion, JavaScript not working - Stack Overflow

programmeradmin0浏览0评论

I am trying to fix the subsequent items after shipping on this page. The first 3 works but after the next heading it does not click. Why?

Is it because the script is breaking at a certain point. Please tell me how to fix this . It is saying my post is mostly code so i will keep adding more words

<!-- JS -->
<script type="text/javascript">
  $(document).ready(function($) {
    $('#accordion').find('.accordion-toggle').click(function() {

      //Expand or collapse this panel
      $(this).next().slideToggle('fast');

      //Hide the other panels
      $(".accordion-content").not($(this).next()).slideUp('fast');
    });
  });
</script>

<!-- CSS -->
<style>
  .accordion-toggle {
    cursor: pointer;
  }

  .accordion-content {
    display: none;
  }

  .accordion-content.default {
    display: block;
  }
</style>

<!-- HTML -->
<h2>Orders</h2>

<div id="accordion">
  <meta charset="utf-8" />
  <h4 class="accordion-toggle"><span>What methods of Payment do you accept?        
    </span></h4>
  <div class="accordion-content default">
    <p>We accept all of the following: Visa, Mastercard, American Express credit and debit cards and PayPal. We also offer our customers Afterpay (see terms via ). All prices on our site are stated in Australian dollars (AUD).
      .</p>
  </div>
</div>

<h2>Shipping</h2>
<div id="accordion">
  <h4 class="accordion-toggle">HOW LONG WILL IT TAKE FOR MY ORDER TO ARRIVE?</h4>
  <div class="accordion-content default">
    <p>Cras malesuada ultrices augue molestie risus.</p>
  </div>
  <h4 class="accordion-toggle">Who do you ship with?</h4>
  <div class="accordion-content">
    <p>Our orders are sent by Australia Post.

So on and so forth but it does not work

I am trying to fix the subsequent items after shipping on this page. The first 3 works but after the next heading it does not click. Why?

Is it because the script is breaking at a certain point. Please tell me how to fix this . It is saying my post is mostly code so i will keep adding more words

<!-- JS -->
<script type="text/javascript">
  $(document).ready(function($) {
    $('#accordion').find('.accordion-toggle').click(function() {

      //Expand or collapse this panel
      $(this).next().slideToggle('fast');

      //Hide the other panels
      $(".accordion-content").not($(this).next()).slideUp('fast');
    });
  });
</script>

<!-- CSS -->
<style>
  .accordion-toggle {
    cursor: pointer;
  }

  .accordion-content {
    display: none;
  }

  .accordion-content.default {
    display: block;
  }
</style>

<!-- HTML -->
<h2>Orders</h2>

<div id="accordion">
  <meta charset="utf-8" />
  <h4 class="accordion-toggle"><span>What methods of Payment do you accept?        
    </span></h4>
  <div class="accordion-content default">
    <p>We accept all of the following: Visa, Mastercard, American Express credit and debit cards and PayPal. We also offer our customers Afterpay (see terms via https://www.afterpay..au). All prices on our site are stated in Australian dollars (AUD).
      .</p>
  </div>
</div>

<h2>Shipping</h2>
<div id="accordion">
  <h4 class="accordion-toggle">HOW LONG WILL IT TAKE FOR MY ORDER TO ARRIVE?</h4>
  <div class="accordion-content default">
    <p>Cras malesuada ultrices augue molestie risus.</p>
  </div>
  <h4 class="accordion-toggle">Who do you ship with?</h4>
  <div class="accordion-content">
    <p>Our orders are sent by Australia Post.

So on and so forth but it does not work

Share Improve this question edited Jul 5, 2018 at 8:33 user7637745 9852 gold badges14 silver badges27 bronze badges asked Jul 5, 2018 at 7:02 Jaemie Dela PenaJaemie Dela Pena 111 silver badge1 bronze badge 2
  • use a class instead of an id if you have multiple elements and replace $('#accordion') with $('.accordion') – LS_ Commented Jul 5, 2018 at 7:11
  • your js definition has to be behind the html markup. put all your script tags in front of the closing </body> tag – mtizziani Commented Jul 5, 2018 at 7:22
Add a ment  | 

5 Answers 5

Reset to default 2

ID selector is the unique selector and it should be unique, but you used it several times in the same page, i think this may be the problem. You can change id to class and have a try.

This solves the issue:

$('.accordion-toggle').click(function(){

Also please use class name accordion instead. IDs are supposed to be unique.

Try adding :

$(".accordion-toggle").click(function(){
        $(this).slideToggle('fast');
    });

  
  $(".accordion-toggle").click(function(){
    $(this).slideToggle('fast');
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- CSS -->
<style>
  .accordion-toggle {cursor: pointer;}
  .accordion-content {display: none;}
  .accordion-content.default {display: block;}
</style>




<!-- HTML -->



<h2>Orders</h2>

<div id="accordion"><meta charset="utf-8" />

<h4 class="accordion-toggle"><span>What methods of Payment do you accept?        
</span></h4>
<div class="accordion-content default">
<p>We accept all of the following: Visa, Mastercard, American Express credit         
and debit cards and PayPal. We also offer our customers Afterpay (see terms via 
https://www.afterpay..au). All prices on our site are stated in Australian 
dollars (AUD). .</p>
</div>


</div>



<h2>Shipping</h2>

<div id="accordion">
<h4 class="accordion-toggle">HOW LONG WILL IT TAKE FOR MY ORDER TO ARRIVE?</h4>
  <div class="accordion-content default">
    <p>Cras malesuada ultrices augue molestie risus.</p>
  </div>

<h4 class="accordion-toggle">Who do you ship with?</h4>
  <div class="accordion-content">
    <p>Our orders are sent by Australia Post.

You can close all, then after open the current clicked.

Also, all IDs should be unique, see: https://www.w3schools./html/html_id.asp

       $(document).ready(function($) {
        $('.accordion-toggle').click(function(){
          //Close all
          $(".accordion-content").each(function(){
            $(this).slideUp('fast');
          })
          
           $(this).next().slideToggle('fast');
    
        });
      });
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="accordion">
<meta charset="utf-8">
<h4 class="accordion-toggle"><span>What methods of Payment do you accept?</span></h4>
<div class="accordion-content default" style="display: none;">
<p>We accept all of the following: Visa, Mastercard, American Express credit and debit cards and PayPal. We also offer our customers Afterpay (see terms via https://www.afterpay..au). All prices on our site are stated in Australian dollars (AUD). .</p>
</div>
<h4 class="accordion-toggle">What if something is wrong with my order?</h4>
<div class="accordion-content" style="display: none;">
<p>Did we get your address wrong? Is something not right in your confirmation email? Please let us know by emailing us at [email protected] so that we can update your information prior to us monogramming your order and dispatching it. Please put “wrong order information” in the subject line so we can assist you promptly. Please refer to our terms and conditions for conditions of purchase.</p>
</div>
<h4 class="accordion-toggle">Do you offer AfterPay?</h4>
<div class="accordion-content" style="display: none;">
<p>Yes we do! You can wear now and pay later with payment by Afterpay. You will just need to pay 4 equal amounts fortnightly but still receive your TrvlBoutiq product right away. <br> TrvlBoutiq is not responsible for any overdue payments. All payments after checkout are handled directly by Afterpay. For more information, visit Afterpay’s plete terms &amp; conditions.</p>
</div>
</div>

There are a few things wrong with your code.

  • First, change the accordion id's to class names. Ids may only be used once on a page.
  • Second, and I suspect this is the issue, the closing animation code. $(".accordion-content").not($(this).next()).slideUp('fast'); What you're basically saying here, if I see it right, is: "Find all elements with classname accordion-content on the page. For each, call the slideUp animation for those that do not have a sibling after it."

    What I'd suggest here is to first close all accordion items and then slide open the one that was clicked.

  • Additionally, putting <meta> tags in divs is probably not how you're supposed to use it and probably redundant. Meta tags usually are placed in the <head> section of a page.

  • Perhaps it would be a good idea to put each set of .accordion-toggle and accordion-content elements into a wrapper, e.g.

<div class="accordion-item"> <h4 class="accordion-toggle"></h4> <div class="accordion-content"></div> </div>

(Sorry for the awful formatting, SO seems to have issues with the editor)

This makes it less prone to bugs and allows more control over the individual items.

发布评论

评论列表(0)

  1. 暂无评论