Javascript 拉斐尔画布中的图像回退
我必须绘制一些图像(使用for循环),如果找不到正确的图像,我想加载一个备用图像 我的javascript:Javascript 拉斐尔画布中的图像回退,javascript,jquery,image,raphael,fallback,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' va
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'
):
PS:我使用的是Raphael
纸张层。图像
不返回HTMLImageElement对象,而是某种Raphael对象
但是,它似乎包含对键0下实际图像元素的引用-因此请尝试
image[0]。onerror=…
,它应该正确地将错误处理程序绑定到实际的HTML图像元素。paper\u layer.image
不会返回HTMLImageElement对象,而是某种Raphael对象,但是,它似乎包含对键0下实际图像元素的引用-因此请尝试image[0]。onerror=…
现在它可以工作了,谢谢!好的,太好了,这是一个简短的回答。
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);
};