jquery - AJAX成功设置的动态元素高度

时间:2022-12-04 16:10:46

I have a collapsed list. On Ajax success, list auto-expands.

我有一个折叠列表。在Ajax成功时,列表自动展开。

I'm trying to get height of that expanded list ... and failing.

我想要得到扩展列表的高度……和失败。

Ajax adds dynamically elements to list, so height changes.

Ajax向列表中添加动态元素,因此高度会发生变化。

Here is stripped code:

这是剥夺了代码:

$('form').submit(function() {

   $.ajax({  
        type: "POST",  
        url: "ajax/ajax.php",  
        data: {data:JSON.stringify(data)},  
        dataType: "json",
        success: function(x) {  

            $('#slider' + x.id).children('ul,li').slideDown("slow");

            //this does not return expected height (outerHeight() also tried)
            var t = $('#slider_' + x.id).height();
         } 
    });  

    return false;
});

1 个解决方案

#1


3  

I'm assuming the slideDown animation takes it time and when you've tried to get the height it has already changed.

我假设滑动条动画需要时间,当你试图得到高度时它已经改变了。

You could use a callback for your animation, and get the height after the animation is over:

您可以使用回调动画,在动画结束后获取高度:

$('#slider' + x.id).children('ul,li').slideDown("slow", function() 
  {
   //this executes AFTER the animation is complete.
    var t = $('#slider_' + x.id).height();
  }

#1


3  

I'm assuming the slideDown animation takes it time and when you've tried to get the height it has already changed.

我假设滑动条动画需要时间,当你试图得到高度时它已经改变了。

You could use a callback for your animation, and get the height after the animation is over:

您可以使用回调动画,在动画结束后获取高度:

$('#slider' + x.id).children('ul,li').slideDown("slow", function() 
  {
   //this executes AFTER the animation is complete.
    var t = $('#slider_' + x.id).height();
  }