The code is very simple, it is to dynamically generate input tags to realize that the change event cannot process the same file. It works in Chrome and Firefox, but printing cannot be triggered in IE browser 3. Help! ! !
var button=document.getElementsByClassName('button')[0];
var imgBox=document.getElementsByClassName('imgBox')[0];
button.onclick=function(){
inputImg();
}
function inputImg(){
var input=document.createElement('input');
input.type='file';
input.addEventListener('change',function(e){
console.log(3);
});
input.click();
}
ie
underclick()
cannot operate nodes that are not in the document, so you can add the following statement beforeclick()
To be compatible with
ie9
, useattachEvent
instead ofaddEventListener
.Also
ie9
was not compatible beforegetElementsByClassName
Why does
button
use.onclick
, but the followinginput
uses.addEventListener
?In the addEventListener documentation, traditional Internet Explorer and its attachEvent method are explained:
IE8 and below do not have the addEventListener method. You can use the attachEvent() method to listen for events. Please note that this in the attachEvent callback points to the window
Use the following to bind the event