繁体   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