JavaScript关于读取路径的问题?高手们出来看下
function showImg()
{
var myTime = new Date();
var imageArray = new Array(10);
imageArray[0] = "0.jpg ";
imageArray[1] = "1.jpg ";
imageArray[2] = "2.jpg ";
imageArray[3] = "3.jpg ";
imageArray[4] = "4.jpg ";
imageArray[5] = "5.jpg ";
imageArray[6] = "6.jpg ";
imageArray[7] = "7.jpg ";
imageArray[8] = "8.jpg ";
imageArray[10] = "9.jpg ";
var year = myTime.getYear();
var month = myTime.getMonth() + 1;
var date = myTime.getDate();
var hours = myTime.getHours();
var minutes = myTime.getMinutes();
var seconds = myTime.getSeconds();
if(year < 100)
{
year = "19 " + year;
}
if(month < 10)
{
month = "0 " + month;
}
if(date < 10)
{
date = "0 " + date;
}
if(hours <= 9)
{
hours = "0 " + hours;
}
if(minutes <= 9)
{
minutes = "0 " + minutes;
}
if(seconds <= 9)
{
seconds = "0 " + seconds;
}
var theString = " " + year + month + date + hours + minutes + seconds;
document.getElementById( 'IMG0 ').src = imageArray[theString.charAt(0)];
document.getElementById( 'IMG1 ').src = imageArray[theString.charAt(1)];
document.getElementById( 'IMG2 ').src = imageArray[theString.charAt(2)];
document.getElementById( 'IMG3 ').src = imageArray[theString.charAt(3)];
document.getElementById( 'IMG4 ').src = imageArray[theString.charAt(4)];
document.getElementById( 'IMG5 ').src = imageArray[theString.charAt(5)];
document.getElementById( 'IMG6 ').src = imageArray[theString.charAt(6)];
document.getElementById( 'IMG7 ').src = imageArray[theString.charAt(7)];
document.getElementById( 'IMG8 ').src = imageArray[theString.charAt(8)];
document.getElementById( 'IMG9 ').src = imageArray[theString.charAt(9)];
document.getElementById( 'IMG10 ').src = imageArray[theString.charAt(10)];
document.getElementById( 'IMG11 ').src = imageArray[theString.charAt(11)];
document.getElementById( 'IMG12 ').src = imageArray[theString.charAt(12)];
document.getElementById( 'IMG13 ').src = imageArray[theString.charAt(13)];
setTimeout( "showImg(); ",1000);
}
<body onload= "showImg(); " leftmargin= "35px " topmargin= "20px ">
<img id= "IMG0 " style= "width:35px; height:35px; " />
<img id= "IMG1 " style= "width:35px; height:35px; " />
<img id= "IMG2 " style= "width:35px; height:35px; " />
<img id= "IMG3 " style= "width:35px; height:35px; " /> 年
<img id= "IMG4 " style= "width:35px; height:35px; " />
<img id= "IMG5 " style= "width:35px; height:35px; " /> 月
<img id= "IMG6 " style= "width:35px; height:35px; " />
<img id= "IMG7 " style= "width:35px; height:35px; " /> 日
<img id= "IMG8 " style= "width:35px; height:35px; " />
<img id= "IMG9 " style= "width:35px; height:35px; " />
<img id= "IMG10 " style= "width:35px; height:35px; " />
<img id= "IMG11 " style= "width:35px; height:35px; " />
<img id= "IMG12 " style= "width:35px; height:35px; "/>
<img id= "IMG13 " style= "width:35px; height:35px; " />
</body>
如果我把图片和此网页放在同一路径下就可以正常读取出图片来完成显示
但如果我把图片方在一个IMAGE的文件夹中 如 Image\0.jpg
他就无法正常的读取了 请问大家如何解决这样的问题?
imageArray[0] = "Image\0.jpg ";
我在后面用 alert(imageArray[theString.charAt(1)];)
他反回是 Image 而不是 Image\0.jpg
请问高手如何解决
[解决办法]
try
imageArray[0] = "Image\\0.jpg ";
imageArray[1] = "Image\\1.jpg ";
imageArray[2] = "Image\\2.jpg ";