我有一个html画布,它显示在输入中选择图像时预览图像。这适用于Chrome,但我似乎无法让它在Safari中运行。特别是 - 在Safari中,onchange="previewFile()"
似乎没有调用previewFile函数。输入onchange JavaScript函数调用不工作在Safari中
<canvas id="canvas" width="0" height="0"></canvas>
<h2>Upload a photo </h2>
<input type="file" onchange="previewFile()"><br>
<script type="text/javascript">
// setup the canvas
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
// grab the photo and display in canvas
var photo = new Image();
function previewFile() {
var file = document.querySelector('input[type=file]').files[0];
var reader = new FileReader();
reader.addEventListener("load", function() {
photo.src = reader.result;
canvas.height = photo.height;
canvas.width = photo.width;
ctx.drawImage(photo,0,0);
}, false);
if (file) {
reader.readAsDataURL(file);
}
}
</script>
非常感谢!这解决了问题,你教了我一两件事! – rcrusoe