javascript – HTML5 FileList和JQuery的每一个

我抓住文件列表([ http://www.w3.org/TR/FileAPI/#dfn-filelist]]然后我想使用JQuery的每个函数.
var file_list = $(this).attr('files');
/* Code goes here */
file_list.each(function()
{
/* Code goes here */

我收到以下错误

Uncaught TypeError: Object #<FileList> has no method 'each'

任何的想法?谢谢.

解决方法

这是因为$(this).attr(‘files’);只返回普通的DOM文件列表,而不是jQuery对象.

要么你需要以老式的方式循环它:

for(var i=0,file;file=file_list[i];i++) {
 //do your thing
}

或者你可以使用$.each:

$.each(file_list,function(idx,elm){
     //do your thing
});

请注意,$.each比plain for循环慢,并且在这种情况下为您提供了极少的便利,所以我坚持使用for循环.

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: &lt;span id=&quot...
jQuery 添加水印 &lt;script src=&quot;../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...