【发布时间】:2016-03-25 19:48:22
【问题描述】:
我正在使用jquery在选择图像时在div(#图像预览)中显示图像,并且我无法弄清楚如何获得宽度和图像的高度;我正在使用“this.width”和“this.height”。
这是我的代码:
$(document).ready(function() {
$('input#photo').on('change', function() {
var files = !!this.files ? this.files : [];
if (!files.length || !window.FileReader) return; // no file selected, or no FileReader support
if (/^image/.test(files[0].type)) { // only image file
var reader = new FileReader(); // instance of the FileReader
reader.readAsDataURL(files[0]); // read the local file
reader.onloadend = function() {
$('div#image-preview').css('display', 'inline-block');
$('div#image-preview').css('background-image', 'url(' + this.result + ')'); // set image data as background of div
$('div#image-preview').css('width', this.width); // doesn't work??
$('div#image-preview').css('height', this.height); // doesn't work??
}
}
});
});
【问题讨论】:
标签: javascript jquery filereader