程式碼很簡單,就是動態產生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();
}
为情所困2017-06-26 10:58:54
ie
下click()
不能操作文件中沒有的節點,所以你可以在click()
前加入下面的語句
document.body.appendChild( input );
input.style.display = 'none';
input.click();
要想相容ie9
之前用attachEvent
而不是addEventListener
。
還有ie9
之前不相容getElementsByClassName
学习ing2017-06-26 10:58:54
為什麼 button
使用了 .onclick
,後面的 input
卻用了 .addEventListener
呢?
在 addEventListener 文件的 傳統的 Internet Explorer 及其 attachEvent 方法 有說明:
對於 Internet Explorer 來說,在IE 9之前,你必須使用 attachEvent 而不是使用標準方法
addEventListener。
習慣沉默2017-06-26 10:58:54
IE8以下沒有addEventListener方法 可用attachEvent()方法監聽事件 要注意attachEvent回呼中的this指向的是window哦
大家讲道理2017-06-26 10:58:54
用下面這個來綁定事件
var addEvent = function(elem, type, handler){
if(window.addEventListener){
addEvent = function(elem, type, handler){
elem.addEventListener(type, handler, false);
};
}else if(window.attachEvent){
addEvent = function(elem, type, handler){
elem.attachEvent('on' + type, handler);
};
}
addEvent(elem, type, handler);
};
addEvent(input, "change", function(e){
alert("changed");
});