-4
我正在将图像的路径推入数组中以随机加载它们。我想检查路径是否包含图像。我不使用图片标签orelse onerror事件会给我一个解决方案。那么,有没有办法检查路径是否包含使用JavaScript的图像?检查路径是否包含使用javascript的图像
在此先感谢。
我正在将图像的路径推入数组中以随机加载它们。我想检查路径是否包含图像。我不使用图片标签orelse onerror事件会给我一个解决方案。那么,有没有办法检查路径是否包含使用JavaScript的图像?检查路径是否包含使用javascript的图像
在此先感谢。
我在想是这样的:
var path = "here/there.jpg"; //Your path to the image.
var ext = path.substring((path.length-4), path.length);
//^ This gets the last four characters of the string 'path'.
if(ext == ".jpg"){
arr[yourIndexHere] = path; //Add the image path to the array you're using.
}else{
//Throw an error here.
}
我会检查每个图像的延伸,看它是否是一个众所周知的图像文件类型。
很显然,如果你已经知道你只使用.jpg
作为例子,你可以使用上面的,但是如果你不知道它可能是什么,下面的工作。
var path = "here/there.jpg"; //Your path to the image.
var ext = path.substring((path.length-4), path.length);
//^ This gets the last four characters of the string 'path'.
switch(ext){
case ".jpg":
//Array stuff here.
break;
case ".png":
//Array stuff here
break;
default:
//Do something else here - throw an error maybe.
}
你到目前为止尝试过什么? – Wishnu
请告诉我们你到目前为止所尝试过的。 – Sumurai8
您创建一个图像,看看它是否成功。看到这个线程:http://stackoverflow.com/questions/3744266/how-can-i-test-if-a-url-is-a-valid-image-in-javascript – Reason