addEventListener
for input file select in IE 9 and 10 should trigger after the file selection but it triggers after the second time for file select, that means for the first time if no files are selected then for first select it wont trigger and after that for every file selection the listener event triggers (if different file is selected). My code snippet:
HTML
<input type="file" name="imagefile" id="upload">
JavaScript
var file = document.getElementById("upload");
file.addEventListener("change", handlefileselect, false);
function handlefileselect(event) {
alert("file selected");
}
The code runs fine in Firefox and Chrome but has a problem with IE.
Old IE versions does not support .addEventListener() method, it has a .attachEvent() method instead to add events to elements.
Use the following addEvent method
function addEvent(evnt, elem, func) {
if (elem.addEventListener) // W3C DOM
elem.addEventListener(evnt,func,false);
else if (elem.attachEvent) { // IE DOM
elem.attachEvent("on"+evnt, func);
}
else { // No much to do
elem[evnt] = func;
}
}
var file = document.getElementById("upload");
addEvent('change', file, handlefileselect)
您应该对IE使用attachEvent函数。
file.addEventListener ? file.addEventListener("change", handlefileselect, false) : file.attachEvent("onchange", handlefileselect);
尝试使用它,我没有检查,但是大多数IE问题已通过标头部分中的此标签解决
<meta http-equiv="X-UA-Compatible" content="IE=edge">
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.