首页 > 解决方案 > 打字稿。未捕获的 ReferenceError:显示未在 HTMLInputElement.onchange 中定义

问题描述

我正在尝试开发一个网络应用程序,其中输入获取书籍的文本文件并处理它以及流行词的数量但是发生了以下错误:

未捕获的 ReferenceError:显示未在 HTMLInputElement.onchange 中定义。

请帮帮我!!

import * as _ from 'lodash';

function show(input) {
  const file = input.files[0];

  const reader = new FileReader();
  reader.readAsText(file, 'UTF-8');
  reader.onload = () => {
    const res = _.words(reader.result, /[а-яА-ЯёЁa-zA-Z]{4,}/gim);
    const result = _.flow([
      _.countBy,
      _.toPairs,
      _.partial(_.orderBy, _, 1, 'desc'),
      _.partial(_.take, _, 10),
    ]);

    const txt = result(res).map(([word, num]) => `<tr><td>${word}<td>${num}`).join('');
    const out = document.getElementById('out');
    out.insertAdjacentHTML('beforeend', txt);
  };
}
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>file</title>
</head>
<body>
<div>
    <div id="drop-area">
        Drag and drop file here
    </div>
</div>

<div class="file-read">
    <div class="file-input">
        <label class="custom-file-upload">
            <input type="file" id="input-file" onchange="show(this)" accept=".txt, .pdf, .epub, .fb2">
            Upload a file
        </label>
    </div>

    <a href="">Reset</a>

</div>

<div id="table-div">
    <table id="out"></table>
</div>
</body>
</html>

标签: javascriptdom

解决方案


javascript 如何连接到您的 HTML?我没有看到任何脚本标签或任何东西。如果您以某种方式加载它,请告诉我们如何加载,以便我们了解错误是否是链接。如果它以某种方式链接,则可能在执行脚本之前调用了 onchange,因此尚未定义 show 函数。


推荐阅读