简体   繁体   English

在AJAX调用jQuery后调整div的大小

[英]resizing div after AJAX call jQuery

I am attempting to resize a div after I load in new HTML content via AJAX. 通过AJAX加载新的HTML内容后,我试图调整div的大小。 I have a hashbang linking system that loads up particular HTML snippets if certain hashes are present in the URL. 我有一个hashbang链接系统,如果URL中存在某些哈希值,则会加载特定的HTML代码段。 The div height resizes correctly when links are pressed to load in the content, but the computed height of the loaded content is consistently 20px less when the page is navigated to directly. 当按下链接加载内容时,div的高度可以正确调整大小,但是直接导航到页面时,加载内容的计算高度始终比其小20px。

HTML: HTML:

<div id="help_page_header">
      <h3>Need some help with searches?</h3>
      <ul class="help_tabs">
        <li class="help_tab"><a class="docs_link" rel="recommendations" href="#!/recommendations">Recommendations</a></li>
        <li class="help_tab"><a class="docs_link" rel="whats_hot" href="#!/whats_hot">What's Hot</a></li>
        <li class="help_tab"><a class="docs_link" rel="history" href="#!/history">History</a></li>
        <li class="help_tab"><a class="docs_link" rel="misc" href="#!/misc">Additional Features</a></li>
      </ul>
      </div>
      <div class="help_page_content">
        <p>Select a help topic above!</p>
      </div>
 ...

jQuery: jQuery的:

$(document).ready(function(){
  if(window.location.hash)
  {
    loadContent(window.location.hash)
  }

  $(".docs_link").click(function(e){
    $(".help_page_content").html("")
    var fragment = this.hash
    loadContent(fragment)
  });

  function loadContent(fragment)
  {
    fragment = fragment.slice(1).replace(/!\//g, "");
    $(".help_page_content").load("./"+fragment+".html", function(){
      var height = $("#ajax_page_wrap").outerHeight();
      $(".help_page_content").animate({height: height}, 500);
    });
  }
});

An example to clarify: 一个例子来澄清:

If I navigate to www.homepage.com/docs/ directly, and then click one of the help links to load content, the height is computed fine. 如果我直接导​​航到www.homepage.com/docs/,然后单击帮助链接之一以加载内容,则可以很好地计算高度。 However, if I navigate to www.homepage.com/docs/#!/history, the computed height is always about 20px less than it should be. 但是,如果我浏览到www.homepage.com/docs/#!/history,则计算出的高度总是比其应该少20px。

Is there an image inside the #ajax_page_wrap div that is not yet loaded when you navigate directly there (and the AJAX call is completing before it's loaded)? 当您直接在#ajax_page_wrap div中导航时(在加载之前,AJAX调用已完成),该图像中是否尚未加载?

What about using $(window).load (which waits for all the images to load) instead of $(document).ready (which does not). 如何使用$(window).load (等待所有图像加载)而不是$(document).ready (不这样做)呢? See this answer. 看到这个答案。

Hope the helps! 希望能有所帮助! Lemme know if that's not the issue and I'll keep brainstorming :) Lemme知道这不是问题所在,我将继续集思广益:)

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

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