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