Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery插件在Chrome中刷新时将图像高度降低到1px_Javascript_Jquery_Css_Google Chrome_Jquery Plugins - Fatal编程技术网

Javascript jQuery插件在Chrome中刷新时将图像高度降低到1px

Javascript jQuery插件在Chrome中刷新时将图像高度降低到1px,javascript,jquery,css,google-chrome,jquery-plugins,Javascript,Jquery,Css,Google Chrome,Jquery Plugins,我已经编写了以下jQuery插件,它查找类为“stretch”的所有元素,然后用类“stretch\u this”标识子元素,并使它们都达到父元素中最高的“stretch\u this”元素的高度 $(document).ready(function () { stretchHeight() }); $(window).resize(function () { stretchHeight()} ); function stretchHeight() { $('.st

我已经编写了以下jQuery插件,它查找类为“stretch”的所有元素,然后用类“stretch\u this”标识子元素,并使它们都达到父元素中最高的“stretch\u this”元素的高度

$(document).ready(function () {
    stretchHeight()
});

$(window).resize(function () { 
    stretchHeight()}
);

function stretchHeight() {
    $('.stretch').each(function() {
        var maxHeight = -1;
        jQuery(this).find(".stretch_this").each(function() { //Resets the height so that it can work again on a resize.
            $(this).height('auto');
        });
        jQuery(this).find(".stretch_this").each(function() {
            maxHeight = maxHeight > $(this).height() ? maxHeight : $(this).height();
        });
        jQuery(this).find(".stretch_this").each(function() {
            $(this).height(maxHeight);
        });
        }
    )
}
它在页面加载时运行,也可以在页面调整大小时运行,因为我的总体布局是响应性的,并且元素的高度可能会改变

这就像预期的一样,除了我在Chrome中刷新页面时,它会将以这种方式拉伸的所有图像高度降低到1px;在IE中不会出现这种情况。在Chrome中重新加载页面(即,在地址栏中单击return而不是单击refresh)效果很好-元素按预期调整大小


为什么会这样?这是我代码中的一个错误,还是Chrome的一个怪癖?有什么办法吗。插件是作为WordPress主题的一部分部署的,如果这很重要的话。

建议问题可能是插件在图像加载之前就已经运行了,因为DOM在刷新时加载得更快;这是正确的。因此,使用建议,我将
$(文档)。ready
更改为
$(窗口)。加载
,插件工作正常。

一个建议是,不要在同一个元素上反复使用三个循环,你可以将三个东西放在一个循环中。最有可能的是,当你刷新时,jQuery在加载图像之前运行。@lshettyl-我想知道是否会是这种情况,但为什么它只会在刷新时发生?因为DOM加载可能比图像更快。即使图像被缓存,它们仍然需要在页面上绘制。