javascript - 如何为 IE 正确使用 onchange 元素
问题描述
对于上传文件,我使用这种形式:
<div id="drop_file_zone" ondrop="upload_file(event)" ondragover="return false">
<div id="drag_upload_file">
<p>DROP FILE(S) HERE</p>
<p>or</p>
<p><input class="browse btn" type="button" id="browse" value="Browse" onclick="file_explorer();"></p>
<input type="file" id="selectfile" name="upload" multiple>
</div>
</div>
js的这一部分
function file_explorer() {
document.getElementById('selectfile').click();
document.getElementById('selectfile').onchange = function() {
for (var i=0; i< this.files.length;i++){ // multiple files uploading
fileobj = document.getElementById('selectfile').files[i];
ajax_file_upload(fileobj);
}
};
}
上传在所有浏览器中都有效,除了 IE。我已经发现 IE 和onchange
.
有没有办法可以重写该函数file_explorer
,使其在 IE 中也能正常工作?
解决方案
尝试使用 JavaScript 方法附加 ondrop 功能,请修改您的代码如下:
<head>
<meta charset="utf-8" />
<title></title>
<script>
function myFunction() {
document.getElementById('selectfile').ondrop = function () {
document.getElementById('selectfile').click();
document.getElementById('selectfile').onchange = function () {
for (var i = 0; i < this.files.length; i++) { // multiple files uploading
fileobj = document.getElementById('selectfile').files[i];
//ajax_file_upload(fileobj);
}
};
};
};
</script>
</head>
<body onload="myFunction()">
<div id="drop_file_zone" ondragover="return false">
<div id="drag_upload_file">
<p>DROP FILE(S) HERE</p>
<p>or</p>
<p><input class="browse btn" type="button" id="browse" value="Browse" onclick="file_explorer();"></p>
<input type="file" id="selectfile" name="upload" multiple>
</div>
</div>
</body>
推荐阅读
- regex - 如何区分主题具有相同开头文本的传入邮件?
- angular - 如何使用路由器插座加载 Angular 组件
- python - 情绪分析,极性分数输出重复多次而不是出现一次
- android - 如果对话框选择styles.xml,如何在android中使DatePickerDialog成为屏幕中心?
- javascript - 获取所有数据后使加载更多按钮消失
- python - 在 Python 中将一个列表的末尾添加到另一个列表的末尾 - Leetcode
- python - 使用 IDEA 算法加密 MP3 文件 [UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf4 in position 6: invalid continuation byte]
- spring-boot - saveAll 对于只有一张表来说太慢了
- python-3.x - 为什么在 python 类中不支持 ** 的操作数类型?
- css - 如何更改活动链接上的下划线颜色 - 反应