简体   繁体   English

动态Div Height jQuery.Load()不起作用

[英]Dynamic Div Height jQuery.Load() not working

I'm trying to dynamically change the height of a div container based on the content loaded into it with jQuery.load(). 我试图基于使用jQuery.load()加载到其中的内容来动态更改div容器的高度。 I've tried many different ways of arranging the code, searched and viewed many posts here and read through the documentation on jQuery. 我尝试了许多不同的方式来安排代码,在这里搜索和查看了许多帖子,并阅读了有关jQuery的文档。 My function works if I run it in the firebug console after page load but not when the code is saved and loads with the page. 如果我在页面加载后在firebug控制台中运行该函数,则该函数起作用,但是在保存代码并随页面加载时却不起作用。 The console outputs "0" for all the table heights if the script is run by the page, but it gives the proper heights when I run it in firebug. 如果脚本由页面运行,则控制台将为所有表高度输出“ 0”,但是当我在Firebug中运行脚本时,控制台会给出适当的高度。

The code is below. 代码如下。 Basically, it's a user-defined page where I've pasted this script and the div #element4 sits within jQuery a jQuery tab. 基本上,这是一个用户定义的页面,我在其中粘贴了此脚本,并且div#element4位于jQuery中的jQuery选项卡中。 Can you spot an error in my code or help me get it working? 您能在我的代码中发现错误或帮助我使其正常工作吗?

$("#element4").load("<dbid>?act=API_GenResultsTable&qid=8", function(response, status, xhr) {
 if (status == "success") {
    $('#element4 table').each( function(x, y){
    var max_height = 0;  
    var yHeight = $(y).height();
    max_height = ( yHeight > max_height) ? yHeight : max_height;
    $('#element4').height(max_height);
    console.log(max_height);
  });
  }
 });

You are resetting max_height to zero for each table . 您正在将每个table max_height重置为零。 Try moving that out of the each loop, along with the eventual setting of the height: 尝试将其与最终的高度设置一起移出each循环:

var max_height = 0; // init max
// find tallest table
$('#element4 table').each(function(x, table) {
    var yHeight = $(table).height();
    max_height = (yHeight > max_height) ? yHeight : max_height;
});
$('#element4').height(max_height); // set height
console.log(max_height);

I was able to find a workaround to the issue by using $.load() to load the external content before creating the tabs with $("#tabs").tabs();. 通过使用$ .load()加载外部内容,然后使用$(“#tabs”)。tabs();创建选项卡,我能够找到解决该问题的方法。 Then I used the script similar to what was posted above.. This is run last. 然后,我使用了与上面发布的脚本类似的脚本。 So load content, create tabs, resize the divs. 因此,加载内容,创建选项卡,调整div的大小。

$('#ELEMENTID table').each(function (x, y) { //find table heights in the <div> 
var max_height = 0;
var yHeight = $(y).innerHeight();
max_height = (yHeight > max_height) ? yHeight : max_height;
var compHeight = max_height + 24; //adding 24 because it was the standard height for the smallest of 3 tables.. 
$('#element3').height(compHeight);
console.log(compHeight);

If someone has a better solution, it's much appreciated. 如果有人有更好的解决方案,将不胜感激。

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

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