FileList:item() 方法
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.
备注: 此特性在 Web Worker 中可用。
语法
js
item(index)
参数
index
-
要从列表中检索的文件的从零开始的索引。
返回值
表示所请求文件的 File
对象。
示例
打印文件名
在此示例中,我们使用 item()
选择 FileList
中的第一项。
HTML
html
<input type="file" />
<div class="output"></div>
JavaScript
js
const fileInput = document.querySelector("input[type=file]");
const output = document.querySelector(".output");
fileInput.addEventListener("change", () => {
const fileList = fileInput.files;
if (fileList.length > 0) {
const file = fileList.item(0);
output.textContent = `你选择了:${file.name}`;
}
});
结果
规范
Specification |
---|
File API # dfn-item |
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
item |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- No support
- No support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.