0

以下のスクリプトを使用して開発されたアコーディオンメニューがあります

$(document).ready(function(){

    $('li.button a').click(function(e){
     $(this).addClass('active');

        /* Finding the drop down list that corresponds to the current section: */
        var dropDown = $(this).parent().next();

        /* Closing all other drop down sections, except the current one */
        $('.dropdown').not(dropDown).slideUp('100');
        dropDown.slideToggle('100');

        /* Preventing the default event (which would be to navigate the browser to the link's address) */
        e.preventDefault();     })      

});

メニューがクリックされたときに.activeクラスを追加する必要があるので、jqueryにaddclassを追加しました。これはクラスを追加していますが、問題は、別のメニューがクリックされたときにクラスを削除する必要があることです。トグルクラスも試しましたが、うまくいきませんでした。

また、ページを開いたときにデフォルトで最初のliアイテムを開くようにします。

4

3 に答える 3

0

関数の先頭にこれを追加するだけです:

$('li.button a').click(function(e){
    $('li.button a').removeClass('active');
    $(this).addClass('active');
    ...
});
于 2012-09-27T10:22:30.747 に答える
0

これを試して

$('li.button a').each(function(){
    $(this).removeClass("active").addClass("no-active"); // your non active class
});
于 2012-09-27T10:23:13.770 に答える
0
$('li.button a').click(function(e){
     if($(this).hasClass('active')) // this line verifies if the actual clicked element is already active
         return false;  // you can return false or do another operation here

     $('li.button a.active').removeClass('active');

     $(this).addClass('active');

     /* Finding the drop down list that corresponds to the current section: */ 
        var dropDown = $(this).parent().next(); 

        /* Closing all other drop down sections, except the current one */ 
        $('.dropdown').not(dropDown).slideUp('100'); 
        dropDown.slideToggle('100'); 

        /* Preventing the default event (which would be to navigate the browser to the link's address) */ 
        e.preventDefault();     
}) 
于 2012-09-27T10:23:26.807 に答える