我有可折叠菜单的动态代码。我想在折叠菜单时更改图像
php可折叠菜单的动态代码:

      if($levelAction=="Expand"){
 echo '<li>';
     echo '<a href="#" class="swap-menu"><img id="coll"  src="images/collapsed.gif" hspace = "2"/>'.$B->getAttribute('TITLE').'</a>';
     echo '<ul style="display:none;" class="sub-menu">';
 $groupStarted =1;
}


这是可折叠菜单的javascript:

   $(function(){
    $('.swap-menu').bind('click',function(){
        $('.sub-menu').find("+ ul").slideUp(1);
        $(this).find("+ ul").slideToggle("fast");
    });

       $('.direct-link').bind('click',function(){
        if(!$(this).parents('.sub-menu').length){
            $('.sub-menu').hide();
        }
        var $current=$(this).parent();
        var id=parseInt($current.find('input:hidden[name=menu-id]').val());
        var slider=$current.find('input:hidden[name=slider]').val();
        var min=parseInt($current.find('input:hidden[name=min]').val());
        var max=parseInt($current.find('input:hidden[name=max]').val());
        button_clk(id, slider, min, max);
    });
});

最佳答案

使用addClass和removeClass切换CSS类,并为这些类分配不同的图像。
检查here完成您的代码。

$(function(){
    $('.swap-menu').bind('click',function(){
        $('.sub-menu').find("+ ul").slideUp(1);
        $(this).find("+ ul").slideToggle("fast");
        $('.swap-menu').toggleClass('current');// here you need to create one css class .current and put image you want to display or switch. Make sure you have alternate image for swap-menu which will switch with the current class
    });

       $('.direct-link').bind('click',function(){
        if(!$(this).parents('.sub-menu').length){
            $('.sub-menu').hide();
        }
        var $current=$(this).parent();
        var id=parseInt($current.find('input:hidden[name=menu-id]').val());
        var slider=$current.find('input:hidden[name=slider]').val();
        var min=parseInt($current.find('input:hidden[name=min]').val());
        var max=parseInt($current.find('input:hidden[name=max]').val());
        button_clk(id, slider, min, max);
    });
});​

09-12 08:13