FireFox中的`complete`属性有哪些变通方法?

Fer*_*ide 13 javascript jquery dom

我试图使用jQuery来确定图像是否已正确加载.

以下工作正常(并返回truefalse作为图像的状态),但似乎只在IE中工作,在FireFox中,它似乎总是返回true- 即使状态实际上是不完整的:

    var image = $("img#myImage");
    alert(image[0].complete);
Run Code Online (Sandbox Code Playgroud)

image.complete在JavaScript或jQuery中Firefox的等价物是什么?

dch*_*les 5

您还可以尝试检查img元素的其中一个维度complete:

function isImageLoaded() {
    var theImage = $('#myImage'); 

    if (!theImage.get(0).complete) {
        return false;
    }
    else if (theImage.height() === 0) {
        return false;
    }

    return true;
}
Run Code Online (Sandbox Code Playgroud)

在Firefox中,卸载imgimg具有无效src属性应该具有.height().width()等于0.在Chrome和Opera中,这两种方法似乎都无法正常工作.在这些浏览器theImage.height()中,我的测试总是返回正值.


Cle*_*ton -1

我不知道 Firefox 是否有另一个 prop、方法可以做到这一点,但是,您可以使用此解决方法:

$(文档).ready(函数(){

$("img").each(
//for each image declared in html
function(index)
{
    document.images[index].complete= false;
    document.images[index].onload = function(){ this.complete= true};
});
Run Code Online (Sandbox Code Playgroud)

});

  • 您无法设置图像的完整属性。它将引发错误,因为该属性是只读的。 (3认同)