动态潜水高度jQuery.Load()不工作

Dynamic Div Height jQuery.Load() not working

本文关键字:工作 Load 潜水 高度 jQuery 动态      更新时间:2023-09-26

我正试图根据jQuery.load()加载到div容器中的内容动态更改div容器的高度。我尝试了许多不同的代码排列方式,在这里搜索和查看了许多帖子,并阅读了jQuery上的文档。如果我在页面加载后在firebug控制台中运行我的函数,但当代码保存并与页面一起加载时,我的函数就不起作用了。如果脚本是由页面运行的,控制台会为所有表的高度输出"0",但当我在firebug中运行它时,它会给出适当的高度。

代码如下。基本上,这是一个用户定义的页面,我在其中粘贴了这个脚本,div#element4位于jQuery的一个jQuery选项卡中。你能在我的代码中发现一个错误吗?或者帮我让它正常工作吗?

$("#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);
  });
  }
 });

您正在为每个tablemax_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);

在使用$("#tabs").tabs()创建选项卡之前,我使用$.load()加载外部内容,从而找到了解决此问题的方法;。然后我使用了类似于上面发布的脚本。。这是最后一次运行。所以加载内容,创建选项卡,调整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);

如果有人有更好的解决方案,我们将不胜感激。