Internet Explorer 正在为以下代码生成此错误:
SCRIPT5007: Unable to get value of the property '0': object is null or undefined
for "document.getElementById('files').files[0]
"
...files[0] 为空是正确的,但我不在乎,我怎么能告诉 IE 不在乎呢?
谢谢
Internet Explorer 正在为以下代码生成此错误:
SCRIPT5007: Unable to get value of the property '0': object is null or undefined
for "document.getElementById('files').files[0]
"
...files[0] 为空是正确的,但我不在乎,我怎么能告诉 IE 不在乎呢?
谢谢
var elem = document.getElementById('files'),
file = elem.files && elem.files[0];
如果文件未定义,这将短路并返回undefined
,否则将返回第一个文件。
澄清一点,错误表明这files
是未定义的变量并且访问属性0
会导致错误。如果它是files[0]
它自己,则表达式只会返回undefined
。
尝试...
var file;
if (document.getElementById('files').files){
file = document.getElementById('files').files[0];
}
这已经很老了,但我想为像我一样来到这里的任何人回答
As Dennis says the error indicates that files is an undefined variable and I'm pretty sure that the problem is IE9 doesn't support files property.
As you can see here IE10 is requiered for.
So try this:
var file;
try{
file = document.getElementById('files').files[0];
}catch(error){
file= null;
}
PS: I apologize for my poor English
var file = document.getElementById('files').files[0] || "undefined";