问题在于“ this”,如果我将其更改为“#photo1”,则图像将能够显示。
我输入了一些文件,这就是为什么我需要编辑代码的原因。我不知道发生了什么事。
$(".pro-set-up-photo-preview").hide();
$("#photo1").change(function () {
var reader = new FileReader();
reader.onload = function (e) {
// get loaded data and render thumbnail.
$(this).next().next("img").attr('src', e.target.result);
};
// read the image file as a data URL.
reader.readAsDataURL(this.files[0]);
$(this).next().next("img").show();
});
这是我的代码
的this
在嵌套函数引用FileReader
对象。您应该在reader.onload
回调之前将对#photo1的引用保存在本地变量中:
var photo = $(this);
然后使用photo
代替$(this)
:
reader.onload = function (e) {
// get loaded data and render thumbnail.
photo.next().next("img").attr('src', e.target.result);
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句