我有一個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