【发布时间】:2017-01-06 10:49:35
【问题描述】:
我在输入字段中选择图像后尝试使用画布预览图像,但出现错误,
这个 Html 被生成了,在这个例子中是 3 次:(只显示这三个中的一个)
<input type="file" class="ImageInput">
<img class="input-preview" src="http://placehold.it/654x363" style="width:280px;height:150px;display:none;" />
<canvas class="myCanvas" width="400" height="200"></canvas>
这是我的 Js 函数,它将预览图像:
readURL: function() {
var $input = $(this);
if (this.files && this.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$input.next('.input-preview').attr('src', e.target.result);
var ctx = $input.next('.myCanvas').getContext("2d");
ctx.drawImage($input.next('.input-preview')[0], 0, 0, 400, 200);
}
reader.readAsDataURL(this.files[0]);
}
$(".ImageInput").change(this.readURL);
},
但我收到此错误:
未捕获的类型错误:$input.next(...).getContext 不是函数
在这一行:
var ctx = $input.next('.myCanvas').getContext("2d");
我该如何解决这个问题?
【问题讨论】:
-
getContext是CanvasElement的方法,而不是jQuery-object.. 使用$input.next('.myCanvas').get(0)... -
并且画布不是文件输入的下一个元素
-
@Rayon 感谢您的建议,但它不起作用我收到相同的错误消息
标签: javascript jquery image canvas