How to make Twitter Bootstrap Collapse work on dynamically loaded html using ajax

后端 未结 3 1570
一向
一向 2021-02-14 12:32

I am loading html using ajax call, this html has Bootstrap Collapse. The issue is Collapse is not working, if you click it wil

相关标签:
3条回答
  • My solve is:

        $('.collapse').on('show', function () {
        var $this = $(this);
        if($this.attr('data-href'))
        {
            $this.html($.getJSON($this.attr('data-href'),function(data){
                $this.html(data.html);
                $this.removeAttr('style');
            }));
        }
        })
    
    0 讨论(0)
  • 2021-02-14 12:51

    Just execute this on ajax success event.

    $('[data-toggle="collapse"]').click(function(e){
          e.preventDefault();
          var target_element= $(this).attr("href");
          $(target_element).collapse('toggle');
          return false;
    });
    
    0 讨论(0)
  • 2021-02-14 13:08

    i would suggest you to call

     $(".collapse").collapse() 
    

    inside the ajax's success function after the dynamic html is appended.

    0 讨论(0)
提交回复
热议问题