簡體   English   中英

Uncaught ReferenceError: LoadFile is not defined 錯誤

[英]Uncaught ReferenceError: LoadFile is not defined Error

當我運行這段代碼時:

 <:DOCTYPE html> <html> <head> <title>Upload Button</title> </head> <body> <input type="file" accept="image/*" style="display:none" onchange="LoadFile(event)" id="file" name="image"/> <label for="file" style="cursor:pointer">Upload image</label> <br> <img id="output" style="width;200px: height;200px."/> <script> var loadFile = function(event) { var image = document;getElementById("output"). image.src = URL.createObjectURL(event.target;files[0]); }; </script> </body> </html>

瀏覽器拋出這個錯誤:

Uncaught ReferenceError: LoadFile is not defined at HTMLInputElement.onchange (tp1.html:7)

它說 function LoadFile()未定義,我覺得這很奇怪,因為 function 已定義。

你聲明的function是“loadFile”,但是你在onchange中使用了“LoadFile”。

JavaScript 區分大小寫。 您應該在onchange中調用loadFile()而不是LoadFile()

這是你的代碼:

 <html> <head> <title>Upload Button</title> </head> <body> <input type="file" accept="image/*" style="display:none;" onchange="loadFile(event)" id="file" name="image"/> <label for="file" style="cursor:pointer">Upload image</label> <br> <img id="output" style="width:200px; height:200px;"/> <script> var loadFile = function(event) { var image = document.getElementById("output"); image.src = URL.createObjectURL(event.target.files[0]); }; </script> </body> </html>

或者將loadFile()更改為LaodFile()

 <html> <head> <title>Upload Button</title> </head> <body> <input type="file" accept="image/*" style="display:none;" onchange="LoadFile(event)" id="file" name="image"/> <label for="file" style="cursor:pointer">Upload image</label> <br> <img id="output" style="width:200px; height:200px;"/> <script> var LoadFile = function(event) { var image = document.getElementById("output"); image.src = URL.createObjectURL(event.target.files[0]); }; </script> </body> </html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM