函数 var value = document.getElementById(id).files[0] 在 JavaScript 中做了什么?
What does the function var value = document.getElementById(id).files[0] do in JavaScript?
我正在使用软件 Brackets 进行编码 JavaScript。我遇到了这个
var value = document.getElementById(id).files[0]
我想知道是否有人可以解释它的功能是什么以及我将来如何使用它以及.file[0]
的意义是什么
来自 Forms Section 4.10.5.4 上的 HTML5 规范:
The files
IDL attribute allows scripts to access the element's selected files. On getting, if the IDL attribute applies, it must return a FileList object that represents the current selected files. The same object must be returned until the list of selected files changes. If the IDL attribute does not apply, then it must instead return null.
基本上 .files
returns 脚本可以访问的选定文件的列表 (FileList
)。
根据 W3School,文件 属性 returns 一个 FileList 对象,表示使用文件上传按钮选择的一个或多个文件。
通过FileList对象,可以获取文件的名称、大小和内容
这个属性是只读的。您可以在 W3schools 网站上阅读更多相关信息 here
您还可以学习如何使用它
document.getElementById(id) 会给你一个普通的 JS 对象,它可以同时具有属性(或特性)和方法。首先,您要求的指令是期望此对象具有 "files" 属性.
javascript 中的属性可以是基本类型(如字符串、数字或布尔值)、复杂对象和 数组。
在这种情况下,括号向我们暗示此 属性(文件)是一个数组。 数组元素通过括号内的索引访问。 .files[0] 表示此数组中的第一项。
例如,如果您有这样一个数组:
var myArray = ['first', 'second', 'last']
您可以使用您要求的相同语法访问元素:
myArray[0] //first
myArray[1] //second
myArray[2] //last
在不知道您的指令上下文的情况下,此文件 属性 与 FileUpload 输入控件完美匹配。在这里检查:
https://www.w3schools.com/jsref/dom_obj_fileupload.asp
我正在使用软件 Brackets 进行编码 JavaScript。我遇到了这个
var value = document.getElementById(id).files[0]
我想知道是否有人可以解释它的功能是什么以及我将来如何使用它以及.file[0]
来自 Forms Section 4.10.5.4 上的 HTML5 规范:
The
files
IDL attribute allows scripts to access the element's selected files. On getting, if the IDL attribute applies, it must return a FileList object that represents the current selected files. The same object must be returned until the list of selected files changes. If the IDL attribute does not apply, then it must instead return null.
基本上 .files
returns 脚本可以访问的选定文件的列表 (FileList
)。
根据 W3School,文件 属性 returns 一个 FileList 对象,表示使用文件上传按钮选择的一个或多个文件。
通过FileList对象,可以获取文件的名称、大小和内容
这个属性是只读的。您可以在 W3schools 网站上阅读更多相关信息 here
您还可以学习如何使用它
document.getElementById(id) 会给你一个普通的 JS 对象,它可以同时具有属性(或特性)和方法。首先,您要求的指令是期望此对象具有 "files" 属性.
javascript 中的属性可以是基本类型(如字符串、数字或布尔值)、复杂对象和 数组。
在这种情况下,括号向我们暗示此 属性(文件)是一个数组。 数组元素通过括号内的索引访问。 .files[0] 表示此数组中的第一项。
例如,如果您有这样一个数组:
var myArray = ['first', 'second', 'last']
您可以使用您要求的相同语法访问元素:
myArray[0] //first
myArray[1] //second
myArray[2] //last
在不知道您的指令上下文的情况下,此文件 属性 与 FileUpload 输入控件完美匹配。在这里检查: https://www.w3schools.com/jsref/dom_obj_fileupload.asp