简体   繁体   English

用于满载动态内容的DOM的jQuery事件监听器

[英]jQuery event listener for fully loaded DOM with dynamic content

I'm trying to use jQuery script to align height of two divs. 我正在尝试使用jQuery脚本来对齐两个div的高度。 Everything works fine until I have some dynamic content in one of divs. 一切正常,直到我在其中一个div中有一些动态内容。 When I hardcode some static content in one of divs like: 当我在其中一个div中硬编码一些静态内容时:

<br>asd<br>asd<br> x 20

both divs has the same height property, but when I load some data from DB to one of divs, they are different. 两个div都具有相同的高度属性,但是当我将一些数据从DB加载到一个div时,它们是不同的。

I guess that the problem is in .ready() listener. 我想问题出现在.ready()监听器中。 Documentation says that it fires when DOM is fully loaded but it looks like it's not the truth. 文档说它在DOM完全加载时会触发,但看起来这不是事实。

My question is: what kind of listener or other 'trick' should I use? 我的问题是:我应该使用什么样的听众或其他“技巧”? I think that jquery/javascript solution is cleaner than messing with css and I would like to have this kind of solution. 我认为jquery / javascript解决方案比使用css更简洁,我希望有这种解决方案。

Thanks in advance. 提前致谢。

jquery script: jquery脚本:

$(document).ready(function(){
    var difference = $("#layout-navigation-wrapper").height() - $("#layout-content-wrapper").height();

    if(difference<0)
    {
        var height = $("#layout-content-wrapper").height() -1;
        $("#layout-navigation-wrapper").height(height);
    }
    else if(difference >= 0)
    {
        var height = $("#layout-navigation-wrapper").height() -2;
        $("#layout-content-wrapper").height(height);

    }   
});

jquery in the base work with event document.ready is means when all DOM is ready until here make the jquery code. 基础中的jquery使用事件document.ready是指所有DOM都准备就绪,直到这里生成jquery代码。 is for don't have a option to render jquery code without render jquery library 是没有渲染jquery代码而没有渲染jquery库的选项

if you want to add event just when all the dom is loaded include content and images you need to do this 如果你想在加载所有dom时添加事件包括内容和图像,你需要这样做

$(document).ready(function(){
$(window).load(function(){
    var difference = $("#layout-navigation-wrapper").height() - $("#layout-content-wrapper").height();

    if(difference<0)
    {
        var height = $("#layout-content-wrapper").height() -1;
        $("#layout-navigation-wrapper").height(height);
    }
    else if(difference >= 0)
    {
        var height = $("#layout-navigation-wrapper").height() -2;
        $("#layout-content-wrapper").height(height);

    }   
});
});

You can use window.onload to execute a script once a web page has completely loaded all content including images, script files, CSS files, etc. 一旦网页完全加载了包括图像,脚本文件,CSS文件等在内的所有内容,您就可以使用window.onload来执行脚本。

window.onload = function() {
    var difference = $("#layout-navigation-wrapper").height() - $("#layout-content-wrapper").height();

    if(difference<0)
    {
        var height = $("#layout-content-wrapper").height() -1;
        $("#layout-navigation-wrapper").height(height);
    }
    else if(difference >= 0)
    {
        var height = $("#layout-navigation-wrapper").height() -2;
        $("#layout-content-wrapper").height(height);

    }
};

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

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