繁体   English   中英

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

[英]onclick function returns undefined in IE, works fine in Chrome and Firefox

我有一个这样的 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>

由于这是我的查询在 google 中的第一次点击,但没有给出实际答案:

我将参考我找到实际解决方案的位置: 锚标记中的 onclick 事件在 IE 中不起作用

这指出 IE 是愚蠢的,需要你改变:

onClick="somefunc();"

onClick="javascript:somefunc();"

我刚刚遇到了同样的问题。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM