简体   繁体   English

通过ajax追加元素后的div高度

[英]Height of div after appending elements via ajax

Say I do an ajax call to retrieve some json and this data is appended to div #example, and I want to obtain the height of the #example using $('#example').height(). 假设我执行ajax调用以检索一些json,并将此数据附加到div #example,我想使用$('#example')。height()获得#example的高度。 If I obtain the height right away, all the items may not be appended yet. 如果我立即获得高度,则可能尚未附加所有项目。 I am trying to obtain the height within the success clause of the ajax call at the end after the appending loops, but the appending action must not be completed yet because I keep getting a smaller height than expected. 我试图在附加循环结束后在ajax调用的成功子句中获取高度,但是附加动作必须尚未完成,因为我的高度一直小于预期。 What is the most favorable course of action to have accurate timing in obtaining the height of #example? 在获得#example的高度时要有准确的时间安排,最有利的做法是什么?

Did you try obtaining the height on success of the ajax call? 您是否尝试在ajax调用成功时获得高度?

$.ajax({
  url: '/example',
  dataType: 'json',
  success: function(data) {
    $('#example').height();
  },
  complete: function(data) {
    $('#example').height();
  }
});

If that still doesn't work, try using a setTimeout for 500 miliseconds to makes sure $('#example') has been populated. 如果仍然不起作用,请尝试使用setTimeout 500毫秒来确保已填充$('#example')。 I've also heard of "complete" param for ajax. 我也听说过Ajax的“完全”参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM