Expanding div to scroll height

前端 未结 2 1540
礼貌的吻别
礼貌的吻别 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: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.

提交回复
热议问题