Heim > Fragen und Antworten > Hauptteil
<input type="file" id="file" accept="image/gif, image/jpeg, image/png">
Die Struktur des HTML-Codes ist wie folgt. Wenn in diesem Fall kein Bild mit einem Seitenverhältnis von 1:1 in der Eingabe vorhanden ist, möchte ich per JavaScript auf eine andere Seite wechseln.
P粉0329772072024-02-18 10:46:13
您基本上需要为输入添加一个处理程序,并检查 height/width === 1
,您可以使用此函数来验证它:
const fileUpload = document.getElementById("file"); function validateImage(target) { const reader = new FileReader(); reader.readAsDataURL(fileUpload.files[0]); reader.onload = function (e) { const image = new Image(); image.src = e.target.result; image.onload = function () { const height = this.height; const width = this.width; if (height / width !== 1) { console.log("ASPECT RATIO NOT 1:1"); window.location.href = "#otherpage"; // redirect return false; } // do nothing return true; }; }; }