首页 > 解决方案 > 将“事件”值从 html 文件传递​​到 js 文件

问题描述

如何将“事件”值从 html 文件传递​​到 js 文件。因为在 js 中,它没有定义事件和 openFile 变量。

html文件:

 <form>
     <input type='file' id="poem" accept='text/plain' onchange='openFile(event)' >
 </form> 

.js 文件:

function printPoem(event) {
    var openFile = function(event) {
         var input = event.target;
         var reader = new FileReader();
         reader.onload = function() {
             var text = reader.result;
         }
     }
}

标签: javascripthtml

解决方案


您需要this从 HTML 传递,例如:

<form> <input type='file' id="poem" accept='text/plain' onchange='openFile(this)'></form>

推荐阅读