Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.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 使用JS更改图像源不起作用_Javascript_Html_Src - Fatal编程技术网

Javascript 使用JS更改图像源不起作用

Javascript 使用JS更改图像源不起作用,javascript,html,src,Javascript,Html,Src,我试图改变src的一个形象的基础上一天。但是src没有更新。 有什么问题 <head> <script type="text/javascript"> image = new Array(7); image[0] = 'img/about-img1.jpg'; image[1] = 'img/about-img2.jpg'; image[2] = 'img/about-img3.jpg'; image[3] = 'img/about-img4.jpg'; imag

我试图改变src的一个形象的基础上一天。但是src没有更新。 有什么问题

    <head>
<script type="text/javascript">
image = new Array(7);
image[0] = 'img/about-img1.jpg';
image[1] = 'img/about-img2.jpg';
image[2] = 'img/about-img3.jpg';
image[3] = 'img/about-img4.jpg';
image[4] = 'img/about-img1.jpg';
image[5] = 'img/about-img4.jpg';
image[6] = 'img/about-img5.jpg';
var currentdate = new Date();
var imagenumber = currentdate.getDay();
document.getElementById("special").src=image[imagenumber];
</script>
</head>
<body>
<img  id="special" src="">
</body>

图像=新阵列(7);
图像[0]=“img/about-img1.jpg”;
图像[1]=“img/about-img2.jpg”;
图像[2]=“img/about-img3.jpg”;
图像[3]=“img/about-img4.jpg”;
图像[4]=“img/about-img1.jpg”;
图像[5]=“img/about-img4.jpg”;
图像[6]=“img/about-img5.jpg”;
var currentdate=新日期();
var imagenumber=currentdate.getDay();
document.getElementById(“special”).src=image[imagenumber];

您正在尝试在加载DOM之前访问它。将脚本标记放在

图像=新的图像阵列(7);
图像[0]=“img/about-img1.jpg”;
图像[1]=“img/about-img2.jpg”;
图像[2]=“img/about-img3.jpg”;
图像[3]=“img/about-img4.jpg”;
图像[4]=“img/about-img1.jpg”;
图像[5]=“img/about-img4.jpg”;
图像[6]=“img/about-img5.jpg”;
var currentdate=新日期();
var imagenumber=currentdate.getDay();
document.getElementById(“special”).src=image[imagenumber];
或者,使用
window.onload
事件确保在加载DOM后执行脚本(类似于jQuery的
$(function(){});
但使用纯JavaScript)。

请参见

您试图在加载DOM之前访问它。将脚本标记放在

图像=新的图像阵列(7);
图像[0]=“img/about-img1.jpg”;
图像[1]=“img/about-img2.jpg”;
图像[2]=“img/about-img3.jpg”;
图像[3]=“img/about-img4.jpg”;
图像[4]=“img/about-img1.jpg”;
图像[5]=“img/about-img4.jpg”;
图像[6]=“img/about-img5.jpg”;
var currentdate=新日期();
var imagenumber=currentdate.getDay();
document.getElementById(“special”).src=image[imagenumber];
或者,使用
window.onload
事件确保在加载DOM后执行脚本(类似于jQuery的
$(function(){});
但使用纯JavaScript)。

请参见解决方案是将javaScript代码放在

var image=[1,2,3,4,1,4,5],
currentdate=新日期();
document.getElementById(“special”).src='img/about img'+image[currentdate.getDay()]+'.jpg';

解决方案是将javaScript代码放在

var image=[1,2,3,4,1,4,5],
currentdate=新日期();
document.getElementById(“special”).src='img/about img'+image[currentdate.getDay()]+'.jpg';

什么是
ImageArray
?也许您的意思是
数组
。您是否使用母版页来构建页面?如果是这样,元素的名称可能会在ID的前面附加“MainContent”,从而阻止您使用getelementbyID查找它…什么是
ImageArray
?也许您的意思是
数组
。您是否使用母版页来构建页面?如果是这样,元素的名称可能会在ID的前面附加“MainContent_u2;”,从而阻止您使用getelementbyID查找它…是的。。这就是问题所在。谢谢+1是的。。这就是问题所在。谢谢+1是的。。这就是问题所在。谢谢+1是的。。这就是问题所在。谢谢你
<body>
<img  id="special" src="">

<script type="text/javascript">
image = new ImageArray(7);
image[0] = 'img/about-img1.jpg';
image[1] = 'img/about-img2.jpg';
image[2] = 'img/about-img3.jpg';
image[3] = 'img/about-img4.jpg';
image[4] = 'img/about-img1.jpg';
image[5] = 'img/about-img4.jpg';
image[6] = 'img/about-img5.jpg';
var currentdate = new Date();
var imagenumber = currentdate.getDay();
document.getElementById("special").src=image[imagenumber];
</script>
</body>
<head>
</head>
<body>
  <img  id="special" src="">
  <script type="text/javascript">
    var image = [1, 2, 3, 4, 1, 4, 5],
        currentdate = new Date();
    document.getElementById("special").src = 'img/about-img' + image[currentdate.getDay()] + '.jpg';
  </script>
</body>