Html 使用后台url()的onerror事件
如果找不到源图像,是否有方法显示备用图像?我知道要做到这一点,需要做如下工作:Html 使用后台url()的onerror事件,html,css,image,background,onerror,Html,Css,Image,Background,Onerror,如果找不到源图像,是否有方法显示备用图像?我知道要做到这一点,需要做如下工作: <img src="imagenotfound.gif" alt="Image not found" onError="this.src='imagefound.gif';" /> 但是,如果您正在执行类似的操作,您如何捕获是否存在错误或是否未找到图像 <div style="background:url('myimage.gif')"></div> 如果myimage.g
<img src="imagenotfound.gif" alt="Image not found" onError="this.src='imagefound.gif';" />
但是,如果您正在执行类似的操作,您如何捕获是否存在错误或是否未找到图像
<div style="background:url('myimage.gif')"></div>
如果
myimage.gif
不透明,可以使用多个背景:
background:url('myimage.gif')、url('fallback.gif');
这样,只有在myimage.gif
不可用时,fallback.gif
才会可见
请注意,fallback.gif
可以下载,即使myimage.gif
可用
或者,尽管未得到广泛支持,CSS Images 3引入了:
background:image('myimage.gif','fallback.gif');
可以用逗号分隔多个字符,以
在哪种情况下,函数表示第一个不是
对于背景图像,没有事件;你得检查一下自己
,如果您得到一个有响应或根本没有响应(超时后),请使用另一个映像资源。此方法受到限制。您也可以使用虚拟映像,并从此处使用OneError事件
$imageFoo = '
<div id="' . $uniqueId . '"
style="
background-image: url(//foo.lall/image.png);
-webkit-background-size: contain;
-moz-background-size: contain;
-o-background-size: contain;
background-size: contain;
background-position: center;
background-repeat: no-repeat;
"
></div>
<!-- this is a helper, only needed because "background-image" did not fire a "onerror" event -->
<img style="display: none;"
src="//foo.lall/image.png"
onerror="var fallbackImages = $(this).data(\'404-fallback\'); $(\'#' . $uniqueId . '\').css(\'background-image\', \'url(\' + fallbackImages + \')\');"
data-404-fallback="//foo.lall/image_fallback.png"
>
';
$imageFoo=”
';
这很挑剔,但是第二个图像的更好名称应该是imagenotfound.gif
,因为它是在第一个图像未显示时显示的available@oriol当第一个映像未显示时,控制台中是否存在阻止404消息的方法found@jpussacq根据,94.58%的浏览器支持多种背景。CSS3尚未实际实现image()。该引用仅针对建议的候选规范。实际上,image()已被推迟到CSS 4图像:目前完全不受支持: