Expanding div to scroll height

前端 未结 2 1541
礼貌的吻别
礼貌的吻别 2021-01-29 05:55

I need to expand a div that has overflow set to hidden via css. When clicking on «expand» the hight of the div is expanded to the hight necessary to display the whole text. To a

相关标签:
2条回答
  • 2021-01-29 06:45

    your code does not cover DOM changes. Use the following:

    $( ".aufklappen" ).on("click", function() {
      ...
    });
    
    0 讨论(0)
  • 2021-01-29 06:49

    Use a delegated event handler bound to the container/body:
    try this:

    $('body').on('click', '.aufklappen', function(){
      $('#hauptTextInhaltReduziert').animate({
        height: $('#hauptTextInhaltReduziert').get(0).scrollHeight
       }, 500, function(){
       $(this).height('auto');
      });
    });
    


    That is, bind to an element that exists at the moment that the JS runs (body exists when the page loads), and supply a selector in the second parameter to .on(). When a click occurs on body element jQuery checks whether it applied to a child of that element which matches .aufklappen the selector.

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