Calculating height using jQuery differs in Firefox and Chrome

孤人 提交于 2019-12-04 07:42:45

In Chrome, the height of the div does not include the height of your 300-pixel tall image "sheli.jpg" because it isn't specified anywhere in the html or css. If you specify the height="300" in your <img> tag or height: 300px; as part its style, it will work.

If is because your image at that time is not yet loaded, therefore the height is 0. That explains the height deficit you got in Chrome. To solve this problem, put the piece of code that set the height inside jQuery(window).load() like this:

jQuery(window).load(function(){
     jQuery("#div1").css("height", jQuery("#div2").height());
});

Per the discussion Justin and I had in the comments above wrapping the jQuery code in $(window).load() will allow this code to execute properly after the images have loaded completely.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!