拉斐尔在画布上的图像后备

时间:2017-12-11 11:00:18

标签: javascript jquery image raphael fallback

我必须绘制一些图像(使用for循环),如果无法找到正确的图像,我想加载一个后备图像。

我的javascript:

var image = paper_layer.image(image_selected, x, y, width, height);
$('image').one('error', function () {    
    console.log('image not found')
    image_selected='/my_path/not_found.png'
    var image = paper_layer.image(image_selected, x, y, width, height);
});

问题在于,当错过图像时,此功能会为每个图像添加一个后备图像,甚至是找到的图像。

我也试过了,但它不起作用(没有回退,消息'image not found'不会出现在控制台上):

var image = paper_layer.image(image_selected, x, y, width, height);
image.onerror=function () {
    console.log('image not found')
    image_selected='/my_path/not_found.png'
    var image = paper_layer.image(image_selected, x, y, width, height);
};

PS:我正在使用Raphael

1 个答案:

答案 0 :(得分:1)

paper_layer.image不返回HTMLImageElement对象,而是返回某种Raphael对象。

然而,它似乎包含对键0下的实际图像元素的引用 - 所以请尝试image[0].onerror=...,这应该正确地将错误处理程序绑定到实际的HTML图像元素。

相关问题