javascript - onclick 函数在 IE 中返回 undefined,在 Chrome 和 Firefox 中工作正常

标签 javascript html internet-explorer

我有一个这样的 HTML 页面(已简化)

<html>
  <head>
    <style>
...
    </style>
    <script type ="text/javascript">
      function foo()
      {
...
      }
    </script>
  </head>
  <body>
    <input type="button" value="ClickMe" onClick="foo()"/>
  </body>
</html>

这在 Chrome、Firefox 和 Safari 上运行良好,但当我使用 IE 时,当我单击时,它在控制台日志中返回“foo”未定义。 我尝试将所有部分都移到里面,但仍然遇到同样的问题。 有什么提示吗?

编辑 1:这是我的更多代码

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
    <meta content="utf-8" http-equiv="encoding"/>
    <title>My Title</title>

    <script type="text/javascript">
      function handleBrowseClick()
      {
        var fileinput = document.getElementById("browse");
        fileinput.click();
      }

      function handleChange()
      {
        var fileinput = document.getElementById("browse");
      }

      function addListener()
      {
        document.getElementById('browse').addEventListener('change', handleFileSelect, false);
      }

      function handleFileSelect(evt)
      {
        var files = evt.target.files;

        var output = [];
        for (var i=0 ; f=files[i] ; i++) {
          var reader = new FileReader();
          reader.onload = (function(theFile) {
            return function(e) {
              <more JS code here>
            };
          })(f);
          reader.readAsBinaryString(f);
        }
      }
    </script>
  </head>
  <body>
    <input type="file" id="browse" name="fileupload" style="display: none" onChange="handleChange();"/>
    <input type="button" value="ClickMe" id="fakeBrowse" onClick="handleBrowseClick();"/>
    <output id="list"></output>
  </body>
</html>

<script type="text/javascript">
  window.onload = addListener();
</script>

最佳答案

我刚遇到同样的问题。

对我来说,这是因为 HTML 有一些语法错误,Chrome(甚至 Microsoft Edge)不会提示,但 Internet Explorer 会提示。

一旦我修复了那些未定义的函数错误就消失了。

关于javascript - onclick 函数在 IE 中返回 undefined,在 Chrome 和 Firefox 中工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43858963/

相关文章:

javascript - 将表格插入内容可编辑的 div

Facebook 没有从 url 中抓取元数据

javascript - jQuery 计算输入的内容

Javascript 生成的 .ics 文件可以在 Chrome 和 Firefox 中打开,但不能在 IE 中打开

css - 在 IE 上,CSS 字体仅在通过内部链接导航时有效

javascript 在运行时删除函数以避免 "multicall"

javascript - 来自 URL 的 src 图像有时会上下颠倒或横向显示

javascript - 如何使用 javascript 和/或 html 在 div 中显示返回的 html 页面

html - 了解 CSS 选择器优先级/特异性

jquery - 使用 jQuery 阻止 iFrame 内的 F5