代码很简单,就是动态生成input标签,来实现change事件无法处理相同文件。在chrome,firefox中都有效,但在ie浏览器中无法触发打印3.求助!!!
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();
}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
ie下click()不能操作文档中没有的节点,所以你可以在click()前添加下面的语句要想兼容
ie9之前用attachEvent而不是addEventListener。还有
ie9之前不兼容getElementsByClassName为什么
button使用了.onclick,后面的input却用了.addEventListener呢?在 addEventListener 文档的 传统的 Internet Explorer 及其 attachEvent 方法 有说明:
IE8及以下没有addEventListener方法 可用attachEvent()方法监听事件 要注意attachEvent回调中的this指向的是window哦
用下面这个来绑定事件