Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果img src工作不正常,则显示替代图像_Javascript_Html_Image - Fatal编程技术网

Javascript 如果img src工作不正常,则显示替代图像

Javascript 如果img src工作不正常,则显示替代图像,javascript,html,image,Javascript,Html,Image,我在一个网页上工作,我有这个功能,它显示了一个家长网站当天的图片 function LoadPage() { var today = new Date(); var yyyy = today.getFullYear(); var mm = today.getMonth()+ 1; var dd = today.getDate(); var url="http://myparentsite"+yyyymmdd+"/image.jpg"; document.getElementB

我在一个网页上工作,我有这个功能,它显示了一个家长网站当天的图片

function LoadPage() { 

var today = new Date(); 

var yyyy = today.getFullYear(); 

var mm = today.getMonth()+ 1; 

var dd = today.getDate(); 

var url="http://myparentsite"+yyyymmdd+"/image.jpg";
document.getElementById("img").setAttribute("src",url);

}

一天的图片通常设置在早上,所以我在午夜和早上7-8点之间遇到问题,在这段时间里,浏览器将显示“未找到图像”的“?”。 如何将其设置为显示前一天的图像? 我试过了


但是我不知道如何在函数和Html中处理它。

基本上,您需要处理图像上的事件

document.getElementById("img").onError = function() {
    // the image didn't load properly, change the src attribute
    document.getElementById("img").setAttribute("src", url2);
}

document.getElementById("img").setAttribute("src",url);

基本上,您需要处理图像上的事件

document.getElementById("img").onError = function() {
    // the image didn't load properly, change the src attribute
    document.getElementById("img").setAttribute("src", url2);
}

document.getElementById("img").setAttribute("src",url);

简单的答案是让父站点引用一个固定的图像位置,当您有一个新的每日图像时,然后用新的图像覆盖该图像并归档旧的每日图像

<img src='http://myparentsite/imageOfTheDay.jpg'/>
或者检查请求的日期并确定要显示的图像

 var now = new Date();
 var now_utc_hour = now.getUTCHours();

 url = "http://myparentsite"+yyyymmdd+"/image.jpg";

 if( now_utc_hour > 7 && now_utc_hour < 8 ) "http://myparentsite"+yyyymmdd2+"/image.jpg";

 document.getElementById("img").setAttribute("src",url);
var now=newdate();
var now_utc_hour=now.getUTCHours();
url=”http://myparentsite“+yyyymmdd+”/image.jpg”;
如果(现在协调世界时>7和现在协调世界时<8)”http://myparentsite“+yyyymmd2+”/image.jpg”;
document.getElementById(“img”).setAttribute(“src”,url);

简单的答案是让父站点引用一个固定的图像位置,当您有一个新的每日图像时,然后用新图像覆盖该图像并存档旧的每日图像

<img src='http://myparentsite/imageOfTheDay.jpg'/>
或者检查请求的日期并确定要显示的图像

 var now = new Date();
 var now_utc_hour = now.getUTCHours();

 url = "http://myparentsite"+yyyymmdd+"/image.jpg";

 if( now_utc_hour > 7 && now_utc_hour < 8 ) "http://myparentsite"+yyyymmdd2+"/image.jpg";

 document.getElementById("img").setAttribute("src",url);
var now=newdate();
var now_utc_hour=now.getUTCHours();
url=”http://myparentsite“+yyyymmdd+”/image.jpg”;
如果(现在协调世界时>7和现在协调世界时<8)”http://myparentsite“+yyyymmd2+”/image.jpg”;
document.getElementById(“img”).setAttribute(“src”,url);
试试这个:

    <img src="http://myparentsite/imageOfTheDay.jpg" alt="" onerror="this.src='http://myparentsite/alternateImageOfTheDay.jpg'"/>

-Arpit

试试这个:

    <img src="http://myparentsite/imageOfTheDay.jpg" alt="" onerror="this.src='http://myparentsite/alternateImageOfTheDay.jpg'"/>


-Arpit

我尝试了此操作,但可能缺少一些代码。我应该在哪里调用url2以显示备选图像?编辑我的答案以包括您应该在哪里包含第二个URL。我尝试了此操作,但可能缺少一些代码。我应该在哪里调用url2以显示备选图像?编辑我的答案以包括您应该在哪里包含您的URL第二个网址。