2015-11-02 100 views
0

我有下面的代碼,試圖在我的網站上實現一個手風琴,但它不工作 - 任何人都可以請告知爲什麼(js和css被引用我的網頁頭)?Jquery手風琴沒有在點擊上顯示內容

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) 

;

+0

它不是if條件,你在哪裏檢查裏面去 - !$ this.hasClass(「手風琴處於激活狀態」) –

回答

0
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(); 
    } 
    }) 
}) 

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

+0

增加了plunker以管理爲主動plunker箭頭:) 不錯的代碼! –

+0

感謝您的迴應 - 這可以在.js文件中使用嗎?或者它是否需要包含在頁面主體中。我現在有'accordion.js'添加到網站,它仍然沒有工作... – YorkieMagento

+0

這應該是內部腳本標記或可以直接添加在JS文件中(確保添加該JS文件的引用在您的HTML)。 你能詳細說明什麼不工作,如果在控制檯中有任何錯誤? –

0

試試這個plunker

<!DOCTYPE html> 
<html> 

<head> 
    <script data-require="[email protected]" 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>