簡體   English   中英

jQuery手風琴在單擊時不顯示內容

[英]Jquery accordion not displaying content on click

我有以下代碼,試圖在我的網站上實現手風琴,但是它不起作用-有人可以建議原因(我的網頁開頭同時引用了js和css)嗎?

HTML:

   <dl class="accordion">
     <dt>Answer 1</dt>
     <dd>Details of the answer go here...</dd>
     </dl>
     <dl class="accordion">
     <dt>Answer 2</dt>
     <dd>Details of the answer go here...</dd>
     </dl>

CSS:

    .accordion { margin: 0 0 30px; border-top: 1px solid #DDD; border-right: 1px solid #DDD; border-left: 1px solid #DDD;
-webkit-border-radius: 4px; -moz-border-radius: 4px; border-radius: 4px; }

    .accordion dt { border-bottom: 1px solid #DDD; }

    .accordion dd { display: none; padding: 20px; border-bottom: 1px solid #DDD; }

    .accordion dt { cursor: pointer; padding: 8px 15px; margin: 0; }

    .accordion dt:before { content: "\25B6"; padding-right: 5px; }

    .accordion dt.accordion-active:before { content: "\25BE"; padding-right: 5px; }

    .accordion dt.accordion-active:hover { cursor: default; }

JS:

 (function($) {
    //Hide all panels
    var allPanels = $('.accordion > dd').hide();
    //Show first panel
    $('.accordion > dd:first-of-type').show();
    //Add active class to first panel 
    $('.accordion > dt:first-of-type').addClass('accordion-active');
    //Handle click function
    jQuery('.accordion > dt').on('click', function() {
      //this clicked panel
      $this = $(this);
    //the target panel content
      $target = $this.next(); 

       //Only toggle non-displayed 
       if(!$this.hasClass('accordion-active')){
          //slide up any open panels and remove active class
          $this.parent().children('dd').slideUp();

          //remove any active class
         jQuery('.accordion > dt').removeClass('accordion-active');
          //add active class
          $this.addClass('accordion-active');
          //slide down target panel
         $target.addClass('active').slideDown();

      } 

    return false;
 });

 })(jQuery)

;

jQuery(function() {
  //Hide all panels
  var allPanels = $('.accordion > dd').hide();

  jQuery('.accordion > dt').on('click', function() {
    $this = $(this);
    //the target panel content
    $target = $this.next();

    if ($target.hasClass("in")) {
      $target.slideUp();
      $target.removeClass("in");
    } else {
      jQuery('.accordion > dd').removeClass("in");
      $target.addClass("in");

      jQuery('.accordion > dd').slideUp();
      $target.slideDown();
    }
  })
})

添加一個導航鏈接來管理選定的箭頭。

試試這個矮人

<!DOCTYPE html>
<html>

<head>
    <script data-require="jquery@1.11.3" data-semver="1.11.3" src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>

    <style>
        .accordion {
            margin: 0 0 30px;
            border-top: 1px solid #DDD;
            border-right: 1px solid #DDD;
            border-left: 1px solid #DDD;
            -webkit-border-radius: 4px;
            -moz-border-radius: 4px;
            border-radius: 4px;
        }

        .accordion dt {
            border-bottom: 1px solid #DDD;
        }

        .accordion dd {
            display: none;
            padding: 20px;
            border-bottom: 1px solid #DDD;
        }

        .accordion dt {
            cursor: pointer;
            padding: 8px 15px;
            margin: 0;
        }

        .accordion dt:before {
            content: "\25B6";
            padding-right: 5px;
        }

        .accordion dt.accordion-active:before {
            content: "\25BE";
            padding-right: 5px;
        }

        .accordion dt.accordion-active:hover {
            cursor: default;
        }
    </style>
</head>

<body>
    <dl class="accordion">
        <dt>Answer 1</dt>
        <dd>Details of the answer go here...</dd>
    </dl>
    <dl class="accordion">
        <dt>Answer 2</dt>
        <dd>Details of the answer go here...</dd>
    </dl>

    <script>
        (function($) {
            //Hide all panels
            var allPanels = $('.accordion > dd').hide();
            //Show first panel
            // commenting this
            // $('.accordion > dd:first-of-type').show();
            //Add active class to first panel 
            // $('.accordion > dt:first-of-type').addClass('accordion-active');
            //Handle click function
            jQuery('.accordion > dt').on('click', function() {
                //this clicked panel
                $this = $(this);
                //the target panel content
                $target = $this.next();

                //Only toggle non-displayed 
                if (!$this.hasClass('accordion-active')) {
                    // hide all dd's
                    $('.accordion > dd').hide();
                    //slide up any open panels and remove active class
                    $this.parent().children('dd').slideUp();

                    //remove any active class
                    jQuery('.accordion > dt').removeClass('accordion-active');
                    //add active class
                    $this.addClass('accordion-active');
                    //slide down target panel
                    $target.addClass('active').slideDown();

                }

                return false;
            });

        })(jQuery)
    </script>
</body>

</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM