javascript - 如何访问 .file 数组?

标签 javascript jquery html

这是我目前的代码。我不确定如何访问 jquery 对象或事件的 .files 数组。

var imageSlots = {
    crx: null,
    fileInput: $('<input type="file"/>'),
    imgNode: new Image,
    editingSlot: 1,
    slot1: null,
    slot2: null,
    slot3: null,
    slot4: null
};

$(document)
.on('ready', function(){
    imageSlots.ctx = document.getElementById('editCanvas').getContext('2d');

    imageSlots.fileInput.on('change', function(e){
        console.log(e);
        imageSlots.imgNode.src = URL.createObjectURL(e.files[0]);
    });

    imageSlots.imgNode.onload = function(){
        imageSlots.ctx.drawImage(imageSlots.imgNode, 0,0);
    };

})
.on('click', '.image', function(){
    imageSlots.fileInput.click();
});

最佳答案

尝试使用this.files,如下:

....
imageSlots.fileInput.on('change', function(e){
    console.log(e);
    imageSlots.imgNode.src = URL.createObjectURL(this.files[0]);
});
....

关于javascript - 如何访问 .file 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25064092/

相关文章:

javascript - Metro UI 动态磁贴 - https ://metroui. org.ua

javascript - 检测对 HTML5 &lt;input form =""> 属性的支持

选项卡中的 jQuery EasyUI 选项卡和 Ajax

html - 如何修改 bootstrap 3 导航栏响应尺寸

javascript - 如何仅获取特定国家/地区的地理谷歌图表

javascript - Medialize/sass.js 是否可以修改 sass 变量?

javascript - 如何在翻转时完成双 div 交换?

javascript - 添加带有输入文本框和不带有输入文本框的表格列单元格

html - Font Awesome 图标之间的间距

html - 在 div bootstrap 中将段落的图像左右对齐