【问题标题】:Is there a way to tell if an image src is returning null inside of javascript? [duplicate]有没有办法判断图像 src 是否在 javascript 中返回 null? [复制]
【发布时间】:2012-07-31 20:25:12
【问题描述】:

在 javascript 中是否有测试图像的 src 是否真的命中真实文件?

我的想法是,在 DOM 构造的某个时刻,浏览器必须意识到文件位置没有返回真实文件,因为它默认为损坏的文件。

【问题讨论】:

    标签: javascript image dom


    【解决方案1】:

    我在图书馆中针对此事件使用的函数:

    function onImageError (source) {
        //NOTE: Image Error function 
        //Useage: <img src="" onerror="onImgError(this)" />
    
        source.src = "/public/images/global/backup_product.jpg"; // location of sample/error image
        source.onerror = ""; // disable onerror to prevent endless loop
        return true;
    }
    

    【讨论】:

      【解决方案2】:

      是的,您可以将onerror 事件用于图像元素。

      【讨论】:

        猜你喜欢
        • 2017-02-21
        • 1970-01-01
        • 2020-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-24
        相关资源
        最近更新 更多