Javascript toggle with Bootstrap collapse plugin

后端 未结 3 1728
借酒劲吻你
借酒劲吻你 2021-01-12 00:05

I try to use the toggle function of the Bootstrap collapse plugin programmatically. I manage to toggle a div when I click on the link in accordion-heading but it works only

3条回答
  •  庸人自扰
    2021-01-12 00:35

    I would guess this happened to a lot of people.

    When you call the collapse function (FYI or any bootstrap function), if you pass an object it means that you initiate the collapse. The toggle option only toggles once on invocation (doc).

    You have to call once with the parameters, and then call only with the action, as a string.

    $.each($('#accordion a.accordion-toggle'), function(i, link){
        var $collapsible = $('#collapse' + id_of_a_bean); // Let's be thorough
    
        $collapsible.collapse({
            toggle : true, // May not be necessary anymore
            parent : '#accordion'
        });
    
        $(link).on('click', 
            function(){
                // ...
                $collapsible.collapse('toggle'); // Here is the magic trick
                // ...
            }
        );
    });
    

    Live demo : http://jsfiddle.net/Sherbrow/uycBa/

    Just to be precise, you can only call once the init process, since it aborts if you have already done it on the same element. That's why it worked only one time.

提交回复
热议问题